Batch File Help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Silent99
Posts: 2
Joined: 11 Mar 2011 07:08

Batch File Help

#1 Post by Silent99 » 11 Mar 2011 07:10

Hello there,

I am having some troubles creating a batch file...I have searched and searched and made it a little bit of the way on my own and was hoping someone could help me out.

What I am trying to do is as follows:

Search a directory with many sub directories for a certain file type (lets say a .txt file)
If the script finds this file I would like a Folder created called Sample in the directory the .txt file was found
Then move the .txt file to this Sample folder
And if the script finds the file in a folder already named Sample to do nothing and continue on.

I have this as my batch so far and it works in finding the file and moving it to the Sample folder, the only problem is the Sample folder is created in the root of where i ran the batch file and all .txt files found are moved to this folder. Close but not quite right.

________________________________________________

@echo off & setlocal EnableDelayedExpansion
echo.
for /R %%a in (*.txt) Do (
mkdir "Sample"
move %%a "Sample" )
echo.DONE

______________________________________________________________


An example of the directories I am talking about would look like this.....

Folder1/SubFolder1/12345.txt
/SubFolder2/12345.txt
Folder2/SubFolder1/23456.txt
/SubFolder2/Sample/23456.txt


Thanks in advance for your wisdom.
Regards,
Ryan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File Help

#2 Post by aGerman » 11 Mar 2011 16:39

Maybe something like that:

Code: Select all

@echo off &setlocal

for /f "tokens=* delims=" %%i in ('dir /a-d /b /s *.txt^|findstr /v "\\Sample\\"') do (
  md "%%~dpiSample" 2>nul
  move "%%~i" "%%~dpiSample\"
)


Regards
aGerman

Silent99
Posts: 2
Joined: 11 Mar 2011 07:08

Re: Batch File Help

#3 Post by Silent99 » 11 Mar 2011 16:53

beautiful...thanks so much!
works perfect.

Post Reply