I have a folder with lots of subfolders and I want to create a folder in each of these subfolders. As There is a lot of these folders I was wondering if there was a way of automatically doing this with a bat file.
This should work for your problem
FOR /d %A IN (d:\softwares\files*) DO mkdir %A\source
Run this in the folder in question: it works on all subfolders in the tree.
If you only want to process the immediate set of folders then remove the /s
#echo off
for /f "delims=" %%a in ('dir /b /s /ad ') do md "%%a\folder" 2>nul
echo done
pause
Related
So i have a dir called c:\user\jdoe\desktop\Folder1\
Inside folder1 i have many folders with subfolder and files.
I have a 7-Zip batch that zips all the folders in place.
So now i have subfolders and subfolders.zip inside folder1.
I need a batch that will detele all the non zipped folders but skip the zip files and the .bat file that lives in c:\user\jdoe\desktop\Folder1\
Any ideas?
If i can get a 7-zip batch that will zip fodlers (example subfolder1.zip subfolder2.zip) and then afterwards delete all non zipped folders that would be great.
Been all over the internet and after deleting half my data by testing my own scripts i have now decided to come here.
So after a long collaboration with a friend we came up with this.
Tested this and it is working 100%
REM create list of directories
SET cwd="C:\Users\user\Desktop\New folder"
SET date=date /T
CD %cwd%
DIR /AD /B >dirlist.txt
REM Zip contents of each directory
for /f "tokens=*" %%a in (dirlist.txt) do (
"c:\program files\7-zip\7z.exe" a "%%a.zip" "%%a"
del /s /q "%%a"
rmdir /s /q "%%a"
)
del dirlist.txt
pause
I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..
I'm trying to create a batch file that will do the following:
I have multiple directories and in each one of those directories there is a folder called '06-2015'. I want to create a batch script that will go thru all of those directories and copy the folder '06-2015' and its files and nothing else.
Example:
C:\Files\Accounts\06-2015
C:\Files\Sales\06-2015
C:\Files\IT\06-2015
Is there a way I can create a script that will go something like:
xcopy C:\Files\*\06-2015 C:\Backup\*\06-2015 /s
Or is there a different/better way to do this?
A wildcard characters allowed only in the last path item.
#ECHO OFF
SET "target=06-2015"
IF NOT EXIST c:\backup\%target% MKDIR c:\backup\%target%
FOR /F "delims=" %%G IN ('DIR /B /AD "c:\files"') DO (
if exist "c:\files\%%G\%target%\" (
:: create backup directory if necessary
MKDIR "c:\backup\%%G\%target%\" 2>NUL
XCOPY /S /E /Y "c:\files\%%G\%target%\" "c:\backup\%%G\%target%\"
)
)
Test this: remove the /s and /e if you don't want any folders inside 06-2015 copied.
#echo off
for /d /r "c:\files" %%a in (06-2015?) do (
if /i "%%~nxa"=="06-2015" xcopy "%%a" "C:\Backup\%%~pnxa\" /s/h/e/k/f/c
)
I would like to make a generic script that runs every CMD-file on several subfolders. E.g.
Each subfolder contains 1 CMD file (name is not the same). It's not the same subfolders each time
c:\folder\Folder1
c:\folder\folder2
c:\folder\folder3
etc
So I would like to search all subfolders and run all CMD-files on each subfolder.
This is designed to:
1) look for every .bat file in a tree
2) change to each directory with a batch file, in turn
3) run the .bat file in the folder it is in
#echo off
for /r %%a in (*.bat) do (
pushd "%%~dpa"
call "%%a"
popd
)
If you want to run every .bat file in the tree on every folder:
#echo off
for /r %%a in (*.bat) do (
for /d /r %%b in (*) do (
pushd "%%b"
call "%%a"
popd
)
)
for /f %%a in ('dir /s /b *.cmd') do call %%a
dir /s /b *.bat builds a list of all cmd files
To run all those files in parallel, replace call with start
I need the correct way to get a simple process to repeat till it reaches end of folder. Inside my Main Folder are multiple subfolders. Inside each of these are more subfolders along with a few files.
I need to run the batch from inside the Main folder and have it enter each subfolder in turn and simply run "dir *. > %date%.txt", then do the same to the next subfolder till all are done.
The only part I cannot get to work right is the change directory to each in turn till all are done.
Thanks
#echo off
for /f %%D in ('dir /b /s /ad') do (
pushd %%D
dir *. >"%date%.txt"
popd
)
to append to a existing file you should use >>, try this:
cd /d X:\main &rem put the path to your main folder here
for /r /d %%i in (*) do dir "%%~fi\*.">>"%date%.txt"
This reports all subfolder from X:\main and there subfolder recursively.
Try these options, if you weren't aware of them:
dir "c:\main folder">"%date%.txt"
dir /a-d "c:\main folder">"%date%.txt"
dir /b /s /a-d "c:\main folder">"%date%.txt"
dir /b /s /ad "c:\main folder">"%date%.txt"