MSFN Forum: Need Help Optimizing Batch File - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Need Help Optimizing Batch File Using findstr and a test file that lists files Rate Topic: -----

#1 User is offline   spacesurfer 

  • Pharmassist
  • Group: Patrons
  • Posts: 1,668
  • Joined: 31-July 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 16 December 2007 - 12:07 PM

Here's what I'm trying to do: I want to collect system files (copy them) to a temp folder in order to reshack them. The list of files are in a text file called files.txt. Th e location of the files are in either windows or system32 folder. The files.txt also includes the compressed form so I can also extract them from an ISO automatically. Here's what my text file looks like.

files.txt (Partial list)
%windir%\explorer.exe	explorer.ex_
%windir%\system32\taskmgr.exe taskmgr.ex_
%windir%\system32\shell32.dll shell32.dl_


The first column in text file lists the location where I want the file to come from. The second column is the compressed form in ISO. Thus, I'm using the tokens=1 command to specify the first column to collect system files, and tokens=2 to extract from ISO.

Here is the batch script I use to collect files (copy them) to a temp location.

collect_files.cmd
cd /d %~dp0
if not exist Collected_Files md Collected_Files
DEL "%cd%\Collected_Files\*.*" /q
for /F "Tokens=1" %%i IN ('FINDSTR "." files.txt') DO Call :copyfiles "%%i"
goto :exit

:copyfiles
copy %1 "%cd%\Collected_Files"

:exit


The line I need help with is "for /F "Tokens=1" %%i IN ('FINDSTR "." files.txt') DO Call :copyfiles "%%i"". This works perfectly without errors, but I'm not sure if 'FINDSTR "." files.txt' is the correct way of doing it.

Is there a better way of going down the list of first column than using "."?

This post has been edited by spacesurfer: 16 December 2007 - 12:09 PM



#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 16 December 2007 - 02:34 PM

Does this help?
@ECHO OFF
PUSHD %~DP0
RD/S/Q COLLECTED_FILES 2>NUL
MD Collected_Files
FOR /F %%? IN (FILES.TXT) DO CALL COPY %%? COLLECTED_FILES>NUL 2>&1


#3 User is offline   spacesurfer 

  • Pharmassist
  • Group: Patrons
  • Posts: 1,668
  • Joined: 31-July 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 17 December 2007 - 11:11 AM

The script works, Yzowl. Thanks.

Obviously, I can't use long filenames with spaces - there was only one I had in the list and I used the 8.3 format to circumvent this problem.

I added "Tokens=2" to your FOR command to get the compressed files - seems to work okay.

One question though: what does the 2>NUL do and >NUL do? It seems to prevent the confirmation that the file was copied or not copied.

#4 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 17 December 2007 - 11:31 AM

Yes, that's exactly what it does!

It prevents
  • STDOUT standard output being echoed to the screen by redirecting it to a nul, non-existing, device >NUL or more precisely 1>NUL.
  • STDERR standard errors being echoed to the screen by redirecting it to a nul, non-existing, device 2>NUL or also 2>&1 meaning the same nul device as the STDOUT was redirected.


They can obviously be removed if you wish to see those things echod to the console.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy