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
JDobbsy1987

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
periboob

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
JDobbsy1987

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
periboob

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
JDobbsy1987

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
periboob

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





Similar help and support threads
Thread Forum
Problem with batch file command to edit registry
reg add HTLM\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DontDisplayLastUserName /t REG_DWORD 00000001 /f I am trying to hide all users at logon and I know this is where you change it but for some odd reason the syntax is wrong and I can not figure it out. Can someone please...
Customization
In Need a batch file command
So, I need a command for batch files, or other things that a batch file can link to, that selects another window, say a text window(notepad) and then can be typed into. I've used the vbs scripting with Set oShell = CreateObject("WScript.Shell") oShell.AppActivate "Window name" ...
General Discussion
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Batch file not waiting for command to finish before moving on
Hiya, i wasn't really sure where to put this, and i have Googled it but i can't find a reason why my batch file no longer does what it used to do. According to everything i've read, my batch should stop at IE, until i close IE, whereupon it should continue. This is how it used to behave. Here it...
Software
Elevated command via batch file?
I've set up a Virtual Router as in this page, but what it doesn't tell you is that you need to re-run the elevated command netsh wlan start hostednetwork every time the computer boots or comes out of standby. Is there a way to run an elevated command via a batch file? Later ... I've just...
Network & Sharing
Batch File that grabs file names from folder to insert into batch file
Ok I'm totally at a loss on how to word this so I'm just going to write it out the best that I can... :confused: First I have a batch file that I'm writing that will convert mp4 files to mp3 files (I have a lot) for a buddies band using ffmpeg. Here's the code that I'm using: ffmpeg -i...
General Discussion

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 19:17.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App