Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Batch File - For Command??


13 Sep 2010   #1

Windows 8.1 Pro x64
 
 
Batch File - For Command??

<This post has now been solved>


Hi,

Sorry if this is not the place but does anyone know how to do the following?

I am trying to create a batch file to search a directory for a specified file type and list it in a text file then get the batch file to read that text file and copy all the files it found to a directory.

i have created the following which does work... the problem is, when there is a space in the filename it says it cant find the file, i don't know how to get it to include spaces i would normally by using " ??

Code:
dir /s /b c:\source\*.txt >c:\files2copy.txt
for /f %%i in (c:\files2copy.txt) do xcopy %%i %%j %%k c:\dest
Any help will be greatly appreciated.

My System SpecsSystem Spec
.

13 Sep 2010   #2

Windows 7 Pro 64
 
 

Hi JDobb, is there any reason that you cant do a two step process? Like create your list, then go back and have XCOPY work on the original directory. XCOPY is works fine for me with spaces in the file names, and it is designed to work on multiple files, where it looks like you are using it on individual files in the for loop.

xcopy "*.txt" J:\test\test2\*.*

here is a backup file that I use now and then
Code:
@echo off
@echo Process saver run history
@type 00SaveProcesses.log

echo.
echo this will backup the "Processes" folder to the primary backup (\\LEN\L)
pause
xcopy c:\users\PB\datapart\Processes\*.* \\len\Processes\*.* /s /d /y /f /c
::/y suppress overwrite prompting
::/f display full file names as copying
::/c continues on error
::/s subdirectories  
::/d only newer files
echo complete %date% --- %time% >> 00SaveProcesses.log
I have several other varations of this basic idea if you work best from examples.

Caution, while you are working this out, Use the /L flag on XCOPY to check that it is going to work before running, or in your for loop
Code:
do @echo xcopy %%j ...
to show what it is going to do before you do it. Remember that the commandline has no pity, and it will eat your lunch without asking.

If you want to use the FOR loop, check in help where it describes the /F "options" part. You have to be careful with the "delims=..." section to get it right.

and be especially careful using batch code that you find on the internet
My System SpecsSystem Spec
13 Sep 2010   #3

Windows 8.1 Pro x64
 
 

Hi Periboob,

Thanks for your reply...

I don't want to copy the .txt file it self, i want my batch file to look inside the .txt file and copy the files listed in the .txt file.

e.g.
My .txt file contains:
c:\test\test.txt
c:\new folder\new.txt
c:\old\jamie.txt

so i want my batch file to then go to each of the above files and copy them to my output dir.

Thanks,
JDobbsy1987
My System SpecsSystem Spec
.


13 Sep 2010   #4

Windows 7 Pro 64
 
 

It is in the "options" control for the FOR loop
Try this for example

Here is my xlist file

Code:
J:\Processes\test\rename>type xlist.txt
J:\Processes\test\rename\x 1.txt
J:\Processes\test\rename\xlist.txt
J:\Processes\test\rename\y1.txt
J:\Processes\test\rename\Y2.txt
J:\Processes\test\rename\Y3.txt
and here is the results using my tokens/delims string. Any special characters in the file names may change the results.
Code:
J:\Processes\test\rename>for /f "tokens=1* delims= " %i in (xlist.txt) do @echo copy "%i %j" k:\test\
copy "J:\Processes\test\rename\x 1.txt" k:\test\
copy "J:\Processes\test\rename\xlist.txt " k:\test\
copy "J:\Processes\test\rename\y1.txt " k:\test\
copy "J:\Processes\test\rename\Y2.txt " k:\test\
copy "J:\Processes\test\rename\Y3.txt " k:\test\
I just fumble around with this stuff, not a guru, but as I understand it, the "tokens=1*" part means that your first variable is as expected, and any following %variable like %j gets all the rest of the tokens delimited by spaces ("delims= ") that space between the quote and the paren is the delimeter between variables.

Keep it test folders until you are confident what the results are likely to be. Periboob is not responsible if you fill up your hard drive in a recursive file copy.

I probably should not be helping you keep doing things the old ways, I understand Microsoft is de emphasizing the commandline in favor of the newer scripting tools. But I am too old to learn new tricks.
My System SpecsSystem Spec
14 Sep 2010   #5

Windows 8.1 Pro x64
 
 

Quote   Quote: Originally Posted by periboob View Post
It is in the "options" control for the FOR loop
Try this for example

Code:
J:\Processes\test\rename>for /f "tokens=1* delims= " %i in (xlist.txt) do @echo copy "%i %j" k:\test\
copy "J:\Processes\test\rename\x 1.txt" k:\test\
copy "J:\Processes\test\rename\xlist.txt " k:\test\
copy "J:\Processes\test\rename\y1.txt " k:\test\
copy "J:\Processes\test\rename\Y2.txt " k:\test\
copy "J:\Processes\test\rename\Y3.txt " k:\test\
Thank you...

this example helped a lot and i have managed to do it.

Regards,
JDobbsy1987
My System SpecsSystem Spec
14 Sep 2010   #6

Windows 7 Pro 64
 
 

Quote   Quote: Originally Posted by JDobbsy1987 View Post
...
Thank you...
...
Happy to help.
My System SpecsSystem Spec
Reply

 Batch File - For Command??




Thread Tools




Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 05:31 PM.
Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App
  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33