Batch File Help - Making sub directories

March 11, 2011 at 07:52:22
Specs: Windows XP/Windows 7
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
Folder1/SubFolder2/12345.txt
Folder2/SubFolder1/23456.txt
Folder2/SubFolder2/Sample/23456.txt

once the batch script runs I would like the directories to look like this


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


Thanks in advance for your wisdom.
Regards,
Ryan


See More: Batch File Help - Making sub directories

Report •


#1
March 16, 2011 at 16:12:32
try this..


@echo off & setlocal EnableDelayedExpansion
echo.
for /R %%a in (*.txt) Do (
pushd %%~pa
echo current directory = !cd!
mkdir "Sample"
move %%a "Sample"
popd
)
echo.DONE

Subhash Chandra.


Report •
Related Solutions


Ask Question