# Solved: Batch file question: Search, copy, paste, rename, repeat



## JTS123 (Dec 4, 2012)

I've scoured the interwebs in search of this batch file, but can't seem to find the complete version of it. I've found batch files for finding files / copying them, but not renaming them when they're found. I've found renaming batch files, but not for ones found. Without knowing enough about batch files, I can't splice these together.

What I have at the moment is a server directory with ~2000 folders. Each folder has subfolders, and some of these subfolders contain a .kzz file. I need to find each of these .kzz files and copy them in to a single folder so I can process them again later.

The full path & file name for these files could be something like this: F:\1200213\peng\cons.kzz. What I need to do is find this cons.kzz file (could be named something other than cons), paste it in C:\Results\, and rename it to the main directory it was found (so, 1200213.kzz). If there are multiple .kzz files in \1200213\, just add a (1) or (2), etc., to the file name instead of overwriting.

I also have multiple directories I would like to run this in, so dropping a batch file in the F:\, G:\, I:\, etc., wouldn't be as nice as dropping one in C:\ and pointing to all directories I need it run.

For bonus points, batch file could run 7zip to unzip my *.kzz file, delete the old, rename the new as 1200213.txt (new extension too).

Alternate route:
If I had the full path to each of these files, I could just use excel to spit out a gigantic batch file to spit out exactly what I want. But I don't know how to get the full path to each of these files in a single text file.

Summary:
Primary: Search > Copy > Paste > Rename as main directory number > Repeat
Bonus: Search > Copy > Paste > Run 7zip > Delete zipped .kzz file > Rename unzipped file as directorynumber.txt (new extension > Repeat
Alternate: Search > Copy file path (F:\1200213\peng\cons.kzz) > Paste in C:\Results\Results.txt > Repeat

Thanks for the help.


----------



## Squashman (Apr 4, 2003)

To get the full path with the file name is easy.

```
@echo off
pushd F:\
FOR /F "delims=" %%G in ('dir *.kzz /a-d /b /s') do echo %%~G>>MyFiles.txt
popd
```
If you want a CSV file with the Path Separated from the File name.

```
@echo off
pushd F:\
FOR /F "delims=" %%G in ('dir *.kzz /a-d /b /s') do echo %%~dpG,%%~nxG>>MyFiles.txt
popd
```


----------



## foxidrive (Oct 20, 2012)

And to process drives f g i to find all *.kzz files with full path and place them in a text file:


```
@echo off
for %%a in (f g i) do (
dir "%%a:\*.kzz" /a-d /b /s>>MyFiles.txt
)
pause
```


----------



## Squashman (Apr 4, 2003)

foxidrive said:


> And to process drives f g i to find all *.kzz files with full path and place them in a text file:
> 
> 
> ```
> ...


Nice!

Not sure if I have the intestinal fortitude to attempt the rest of it. That looks like way to much work.


----------



## foxidrive (Oct 20, 2012)

If he can process it in some excel statements then there must be some patterns in it that we aren't aware of.


----------



## JTS123 (Dec 4, 2012)

Right now I'm running your second batch file, Squashman. After a few tests it seems to do the trick nicely. Thank you.

This is not going to be a pretty batch file when I'm done with it. It's going to be long and ugly. But, hopefully, I'll only have to run it once, and I don't care if it runs for a day or two to get the job done. In the end, it's going to be copy <path\file.kzz> to <myfolder> rename <file.kzz> to <1234.kzz>, then repeat for the next file. And the next. And the next thousand. Two thousand.

Where excel comes in to play is I'll be able to paste the path in to one column, and have another column next to it calling out the directory number (\123456\) as the rename for the file. Then link everything together (="copy" & A1 & "rename" & A2 & etc.). Then copy everything down so I end up with my thousands of custom lines for each file. Then copy that custom code in to a batch file. Like I said, not pretty, but I'll only have to do it once.

The alternate to my first post has been solved and I'll mark it as such, but tomorrow - maybe someone would like to challenge themselves to figure out how to do the first question.

Thanks again for the quick responses!


----------



## JTS123 (Dec 4, 2012)

Oh, and thank you for the automatic change too, foxi. I can't confirm if it works or not for me (I'm sure it does). The other drives I need to scan are archived / not accessible at the moment.

By the "I need this to look in another directory" qualification, I meant I don't want to have to run this from the directory I'm reading (write permissions).

By the way, for those wondering, Squash's batch file will drop the MyFiles.txt file in the directory being read. Just put your path behind the MyFiles.txt and you'll be fine.


----------



## foxidrive (Oct 20, 2012)

JTS123 said:


> maybe someone would like to challenge themselves to figure out how to do the first question.


Which is the first question? This?



> The full path & file name for these files could be something like this: F:\1200213\peng\cons.kzz. What I need to do is find this cons.kzz file (could be named something other than cons), paste it in C:\Results\, and rename it to the main directory it was found (so, 1200213.kzz). If there are multiple .kzz files in \1200213\, just add a (1) or (2), etc., to the file name instead of overwriting.


When you say 'paste' does that mean copy or move the file?


----------



## JTS123 (Dec 4, 2012)

Sorry, I'm used to actually having to copy / paste in other programs. I didn't realize that "copy" in a batch file would automatically assume a "paste" portion of it as well. Makes complete sense though.

First question was Find file > copy file > rename file as directory name (so, 1200213 or 1200213(1) as required) > Repeat for the next file

Bonus was Find file > copy file > run 7zip on file (which creates a .kzl file) > rename .kzl file as directory.txt (new extension) > Repeat for next file


----------



## foxidrive (Oct 20, 2012)

> run 7zip on file (which creates a .kzl file) > rename .kzl file as directory.txt (new extension)


Is there precisely one file in each kzz file and that file is a .kzl file?
What is the name of the .kzl file? Is it the same as the .kzz filename?

Will there be any .kzl files in the parent directory off the root?

Please clarify "rename .kzl file as directory.txt (new extension)"

I assume that the directory name is the parent directory off the root that it is found in.
Do you want the .kzl file in this format?

directory.txt
directory-1.txt
directory-2.txt
etc


----------



## dkbod-ld (Dec 5, 2012)

I see an elegant piece of code in the offing....<grin>!


----------



## JTS123 (Dec 4, 2012)

I'm going to try to clarify the extensions.
*.KZZ file = Original file I need to find
*.KZL file = Unzipped *.KZZ file, acts just like an .XML file. I don't know why it's "KLZ".
I should have just used false extensions to make it easier to visualize.

When the KZZ file is unzipped, it creates one file called doc.kzl, and _sometimes_ one folder called "files". The doc.kzl file will never be named something else upon unzipping, and will not be named the same as the parent file. The contents of "files" can be overwritten / deleted, as well as the folder itself.

When the .KZZ file is unzipped and creates the doc.KZL file, I want "doc" to be replaced with the name of the parent .KZZ file. I also want the extension changed from .KZL to .TXT (or XML, haven't decided which, but I can edit that as required). So, if 123.KZZ gets unzipped, it creates doc.KZL. Rename doc.KZL as 123.TXT.

The name of the parent file comes from the name of the directory it was found in. F:\*123456*\Folder\Folder\Folder\*file.KZZ* ==> file.KZZ becomes 123456.KZZ.

(Update on the original batch file - this thing ran overnight and I now have a bunch of file paths to process. Thanks again!)


----------



## foxidrive (Oct 20, 2012)

JTS123 said:


> I should have just used false extensions to make it easier to visualize.


Actually, exact details is always better.



> When the KZZ file is unzipped, it creates one file called doc.kzl, and _sometimes_ one folder called "files". The doc.kzl file will never be named something else upon unzipping, and will not be named the same as the parent file. The contents of "files" can be overwritten / deleted, as well as the folder itself.


ok.



> When the .KZZ file is unzipped and creates the doc.KZL file, I want "doc" to be replaced with the name of the parent .KZZ file. I also want the extension changed from .KZL to .TXT (or XML, haven't decided which, but I can edit that as required). So, if 123.KZZ gets unzipped, it creates doc.KZL. Rename doc.KZL as 123.TXT.
> 
> The name of the parent file comes from the name of the directory it was found in. F:\*123456*\Folder\Folder\Folder\*file.KZZ* ==> file.KZZ becomes 123456.KZZ.


Sounds fair. We find out better information the more times you explain.

This should work. Check the location of the 7zip executable.
No files should have hidden or system attributes.

Place a test kzz file on a USB flash drive and test it. Change (f g h) to the drive you picked.

The current directory needs to be writable for the doc.kzl file during processing.


```
@echo off
for %%z in (f g h) do (
for /f "delims=" %%a in ('dir "%%z:\*.kzz" /a:-d /b /s') do call :next "%%a"
)
echo done
pause
goto :EOF

:next
del "doc.kzl" 2>nul
"c:\Program Files\7-Zip\7z.exe" e "%~1" doc.kzl >nul
set num=-1
:loop
set /a num=num+1
set x=(%num%)
if %num% EQU 0 set x=
if exist "%~d1\%~n1\%~n1%x%.txt" goto :loop
echo moving "%~d1\%~n1\%~n1%x%.txt"
copy /b "doc.kzl" "%~d1\%~n1\%~n1%x%.txt" >nul
del "doc.kzl" 2>nul
```


----------



## foxidrive (Oct 20, 2012)

dkbod-ld said:


> I see an elegant piece of code in the offing....<grin>!


I try. Some would say that I am very trying.


----------



## JTS123 (Dec 4, 2012)

Getting an error when I run it.



> moving "f:\10-11501 New School ref\10-11501 New School ref.txt"
> The system cannot find the path specified
> done
> Press any key to continue...


File name is correct, file extension is incorrect. I don't know enough about the code to fix it.
Jump drive is an encrypted drive, but the file itself isn't in an encrypted folder (other folders are, however). I don't think this would influence it.

Full path of file is: F:\123456\10-11501 New School ref.kzz


----------



## foxidrive (Oct 20, 2012)

JTS123 said:


> File name is correct, file extension is incorrect.
> 
> Full path of file is: F:\123456\10-11501 New School ref.kzz


The filename is incorrect, and the extension is correct - right?

The filename should have been 123456.txt I think. Is that what you expected?


----------



## foxidrive (Oct 20, 2012)

Try this:


```
@echo off
for %%z in (f g h) do (
for /f "delims=" %%a in ('dir "%%z:\*.kzz" /a:-d /b /s') do call :next "%%a"
)
echo done
pause
goto :EOF

:next
for /f "tokens=1,2 delims=\" %%b in ("%~dp1") do set "name=%%c"
del "doc.kzl" 2>nul
"c:\Program Files\7-Zip\7z.exe" e "%~1" doc.kzl >nul
set num=-1
:loop
set /a num=num+1
set x=(%num%)
if %num% EQU 0 set x=
if exist "%~d1\%name%\%name%%x%.txt" goto :loop
echo moving "%~d1\%name%\%name%%x%.txt"
copy /b "doc.kzl" "%~d1\%name%\%name%%x%.txt" >nul
del "doc.kzl"
```


----------



## JTS123 (Dec 4, 2012)

Holy moly. It worked.
Would you be able to add something in there to move all of the created .txt files to a single named directory? (F:\finalresults\ for example).

And if I wanted to change it to .xml (or another format), should I just replace all .txt with .xml?


----------



## foxidrive (Oct 20, 2012)

Change this

if exist "%~d1\%name%\%name%%x%.txt" goto :loop
echo moving "%~d1\%name%\%name%%x%.txt"
copy /b "doc.kzl" "%~d1\%name%\%name%%x%.txt" >nul

to this:

if exist "f:\finalresults\%name%%x%.txt" goto :loop
echo moving "f:\finalresults\%name%%x%.txt"
copy /b "doc.kzl" "f:\finalresults\%name%%x%.txt" >nul

and yes, change .txt to .xml in three places.


----------



## JTS123 (Dec 4, 2012)

I'm stunned. That works perfectly, and so fast. Thank you very much for your help.


...sheesh! Wow.


----------



## foxidrive (Oct 20, 2012)

That's good to hear. Glad it helped.


----------



## JTS123 (Dec 4, 2012)

I've encountered a small problem. The batch file works, but I've encountered a few (read: helluvalot) of archived folders. And I don't have write access to these.

Currently the batch file finds my *.kzz file, extracts the doc.kzl, renames it, then copies it to my main directory. I won't be able to extract to a folder I don't have write access to. Would it be possible to have the batch file find the file, copy it to my main directory, extract, rename, delete the copied *.kzz file? (Or, extract to my main directory)?

(Yes, it would be cleaner to get write access to these archived folders, but IT doesn't see the value in it at the moment so I need to make a workaround).

Thanks in advance!


----------



## foxidrive (Oct 20, 2012)

The batch file only needs write access to the folder that holds the batch file, and f:\finalresults
It does not write to any other folder, but only reads the *.kzz files.

Perhaps if your folders are archived, then that means they are on a different media and inaccessible.
Ask your IT how archived folders works.


----------



## JTS123 (Dec 4, 2012)

Hm, currently the archived section of the server looks like this:


> (this is not a file) \\Nue-q39t-pp\ClosedJobs


 Even if I try to run the batch file with that in there (somehow), it doesn't want to run.

When I was watching this thing run earlier, it would physically drop something in the folder I which had the .kzz file (and delete it at the end). I wouldn't have the rights to do any of that.


----------



## foxidrive (Oct 20, 2012)

How can you see that it drops something in the folder? (it doesn't) It drops a file in the folder with the batch file, and deletes it from the same place after copying it to f:

What are the error messages on the screen - hit control S to stop the screen output and copy/paste into a reply.


----------



## JTS123 (Dec 4, 2012)

Sorry for the delay in responding - XMas break and such.

If you say the file isn't created where the original .kzz file is, I trust you. I guess one of my earlier batch files dropped an extracted file in there and didn't delete it, and that's probably what I saw.

When using the network path as noted above, I get


> The network name cannot be found.


When replacing with O (was "I" before), I get


> File Not Found


I've done searches in this directory and there are a bunch of .kzz files, so it should be working.


----------



## foxidrive (Oct 20, 2012)

There are a couple of issues here:
A) there's an archive issue that was mentioned
B) you are mentioning network paths when the aim was to search local drives.

Please provide more information about these...


----------



## JTS123 (Dec 4, 2012)

A) Archive issue was only that I can't write in archived folders, and I thought that the batch file writes something in the folders. I don't believe this is an issue anymore.
B) I'll need to dig to find out about where these folders are stored. It didn't occur to me that it would be a network path (not too familiar with it). Is it possible to run a batch file across a network?


----------



## foxidrive (Oct 20, 2012)

A) ok. But if folders are archived then they aren't available. Right?
B) A batch file can access network locations, if it has permissions and is written to accommodate it.


----------



## JTS123 (Dec 4, 2012)

The files are available.

I have permission to access / view / copy the files, just no write access.


----------



## foxidrive (Oct 20, 2012)

I don't understand what the issue is, now.


----------



## JTS123 (Dec 4, 2012)

I don't know how to make the batch file run in "\\Nue-q39t-pp\ClosedJobs"


----------



## foxidrive (Oct 20, 2012)

Try this - you need permissions to access the server and the share.

EDIT: this is changed to write the temp file to your desktop and not on the network share.


```
@echo off
pushd "\\Nue-q39t-pp\ClosedJobs" && (
for /f "delims=" %%a in ('dir "\*.kzz" /a:-d /b /s') do call :next "%%a"
echo done
pause
popd
)
goto :EOF

:next
pushd "%userprofile%\desktop\"

for /f "tokens=1,2 delims=\" %%b in ("%~dp1") do set "name=%%c"
del "doc.kzl" 2>nul
echo "name=%name%"
echo "file=%~1"
"c:\Program Files\7-Zip\7z.exe" e "%~1" doc.kzl >nul
set num=-1
:loop
set /a num=num+1
set x=(%num%)
if %num% EQU 0 set x=

if exist "f:\finalresults\%name%%x%.xml" goto :loop
echo moving "f:\finalresults\%name%%x%.xml"
copy /b "doc.kzl" "f:\finalresults\%name%%x%.xml" >nul

del "doc.kzl"
popd
```


----------



## foxidrive (Oct 20, 2012)

The post above has been edited.

I'm not sure if the 'name' will be what you want because it depends on where the files are on the network share. 
It will print the name to the console.


----------



## JTS123 (Dec 4, 2012)

Doesn't seem to be working.

I pointed the batch file directly to where a *.kzz file is on the network so it should take no time at all to run, and it just does nothing. No error messages, no files being copied or created. When I Ctrl Break it, the directory CMD lists is the directory where the *.kzz file is, instead of where the batch file is (which is where it normally returns me to).

I've also tried manually unzipping the file to see if I have rights, and it does work.

Thoughts?


----------



## foxidrive (Oct 20, 2012)

If it doesn't print 'done' or anything else then the network path is not correct, or you don't have permissions for that network path without a username and password.

Absolutely nothing was printed on the screen?


----------



## JTS123 (Dec 4, 2012)

Huh, interesting. I let this thing run for 4 hours and it did spit something out. Instead of spitting out the file that I had pointed it at, it spat out everything on the network. So it does work. I thought it didn't as it should have spat something out almost instantly, given that I had pointed it directly to where a file was.

But, like you said the names of the files aren't working properly. Here is the file structure:



> V:\2012\123456\...\*.kzz


For some reason, the V:\ can change to T:\, O:\, etc., depending on when I look at the directory.

The files it spits out are 2012.xml, 2012(1).xml, etc. instead of 123456.xml. Is there a way to have it name the file as the second folder instead of the first? It will always be the second folder when dealing with this directory. First folder is the year the project took place.


----------



## foxidrive (Oct 20, 2012)

Try this:


```
@echo off
pushd "\\Nue-q39t-pp\ClosedJobs" && (
for /f "delims=" %%a in ('dir "*.kzz" /a:-d /b /s') do call :next "%%a"
echo done
pause
popd
)
goto :EOF

:next
pushd "%userprofile%\desktop\"

for /f "tokens=1,3 delims=\" %%b in ("%~dp1") do set "name=%%c"
del "doc.kzl" 2>nul
echo "name=%name%"
echo "file=%~1"
"c:\Program Files\7-Zip\7z.exe" e "%~1" doc.kzl >nul
set num=-1
:loop
set /a num=num+1
set x=(%num%)
if %num% EQU 0 set x=

if exist "f:\finalresults\%name%%x%.xml" goto :loop
echo moving "f:\finalresults\%name%%x%.xml"
copy /b "doc.kzl" "f:\finalresults\%name%%x%.xml" >nul

del "doc.kzl"
popd
```


----------



## JTS123 (Dec 4, 2012)

I pointed it towards the \2012\ folder. It now only searches in 2012 (instead of 2011, 2010, etc. as before), but it still renames the files as 2012(1).xml, etc.


----------



## foxidrive (Oct 20, 2012)

Show me some of the screen output please.


----------



## JTS123 (Dec 4, 2012)

I have the batch file pointed at "\\Nue-q39t-pp\ClosedJobs\2012"

Output for the last file (of 100):


> "name=2012"
> "file=U:\2012\1201264\Graphics\Open_House\W0078\Lab.kzz"
> moving "C:\Users\jts\Desktop\Test\2012(100).xml"


I've tried pointing the old batch file at the U:\ too to no avail ("File not found").


----------



## foxidrive (Oct 20, 2012)

You have something not quite right in your copy of the batch code. Copy and paste it again.

It should take the third token in the name, as it does in my test.


```
@echo off
for /f "tokens=1,3 delims=\" %%b in ("U:\2012\1201264\Graphics\Open_House\W0078\Lab.kzz") do set "name=%%c"
echo "name=%name%"
pause
```


----------



## JTS123 (Dec 4, 2012)

Hah! Brilliant! That was it.
Thank you again for your help. This now covers _all_ *.kzz files and I shouldn't have to bother you again.


----------



## foxidrive (Oct 20, 2012)

No problemo. I'm glad it's working for you.

It could have issues still with filenames or paths that contain % and ^ characters, if there are any.


----------

