Batch Script - Empty Multiple Directories In Paths With A Pattern - batch-file

I'm looking to write a short batch script that will delete all files within a set of directories. More specifically, suppose I have the top directory "workspace" and it contains several directories beginning with the sting "project" (e.g. project-something, project-another). Then each of these "project" directories contain a "model" directory. I want to have the script empty each of these model directories.
I know this is doesn't work, but I looking for something along the lines of
del project*\model\*
But I know that the * after project will not select all directories starting with project then proceed into the model directories to clear them. What would be a correct way to go about doing this?
Thank you for your time!

Put this into a .bat file and run.
#echo off
for /F "usebackq delims=" %%F in (`dir /ad /s /b model`) do (
del /s /q "%%F"
echo Removed "%%F"
)
pause

Related

Need to write a batch script to copy a single EXCEL file into all sub-folders that the EXCEL file is currently in

I have an Excel template that I will be needing to copy into hundreds of folders. I want to create a batch script that will do this for me.
Here's the catch: I need this to work based on whatever folder the batch file is in. I need a script that can be run from whatever the directory the batch file is in without specifying a specific directory or destination. The script will detect that excel file and then copy it into every subfolder in that directory.
Everything I've found so far requires you to specify the directory and destination. I would like something that can be moved to any computer and work based on the setup shown below:
Is this possible? Any help would be much appreciated! Thank you.
Make the current working directory the one where the .bat file script exists using PUSHD. Go back to the previous directory with POPD.
#ECHO OFF
PUSHD %~dp0
SET "TEMPLATE_FILE=Property Inspection Form TEMPLATE.xlsm"
IF NOT EXIST "%TEMPLATE_FILE%" (ECHO ERROR: No template file found in "%CD%" & GOTO TheEnd)
FOR /F "delims=" %%d IN ('DIR /B /A:D') DO (
COPY /Y "%TEMPLATE_FILE%" "%%~d"
)
:TheEnd
POPD
EXIT /B
If you want to copy any and all .xlsm files to the subdirectories, you could do something like this. There really should be error checking after the COPY command to see that it worked.
#ECHO OFF
PUSHD %~dp0
FOR /F "delims=" %%d IN ('DIR /B /A:D') DO (
COPY /Y "*.xlsm" "%%~d"
)
:TheEnd
POPD
EXIT /B

Q: Batch File to Move Files Based on Characters Before Delimiter in Name

I'm a bit of a novice when it comes to .BAT files. Please, I'm in need of a batch file that will create folders based on the names of files in the local folder, and then move those files into their corresponding folders. The folder names need to be determined by the characters before the delimiter in the file names, in this case an underscore.
For example, it would take these files
june_jahdfjlkaluie2.xlsx
june_jahdfjlkaluie.xlsx
august_nnnvbcnkziery2.xlsx
august_nnnvbcnkziery.xlsx
december_bagjd_kasdgf.xlsx
december_byuueyiuyoi.xlsx
Create these folders
june
august
december
And move the files into those folders based on the characters before the underscore.
This is the code I have so far
#echo off &setlocal
for /f "delims=_" %%i in ('dir /b /a-d *.xls') do (
set "file=%%~i"
setlocal enabledelayedexpansion
set "folder=!file!"
mkdir "!folder!" 2>nul
move "!file!" "!folder!" >nul
)
endlocal
echo Did it work?
Pause
The batch file is able to make the folders based on the file names. However, when it attempts to move the files, it produces an error stating 'The process cannot access the file because it is being used by another process.' I've attempted several fixes, including creating a separate for command for moving the files, and nothing seems to work.
Any advice is greatly appreciated. Thank you.
#echo OFF
SETLOCAL
for /f "delims=_" %%i in ('dir /b /a-d *_*.xlsx') do (
mkdir "%%i" 2>nul
move "%%i_*.xlsx" "%%i" >NUL 2>nul
)
echo Did it work?
GOTO :EOF
would likely work and be easier.
Note that I've changed the target filemask to ….xlsX because the short filename for a .xslx file does not necessarily retain the *_*.xls format.

Batch empty subfolders from folders in a CSV file

I have a very long list of partail names for folders (each partial is unique to a specific folder); what I am looking to do is write a batch file to go through each of the sub-folders within each folder, pull the files to the root of that folder, and replace the files if there's duplication (there is a lot of duplication we're looking to remove).
I've used a couple batch files to move and do other things with these folders, I just can't get the code to work on emptying subfolders.
Batch file 1 (accessing CSV, calling the other batch to do the work):
#echo off
FOR /F "tokens=1,3 delims=," %%G IN (Pt1Test.csv) DO call Empty1.bat %%G
Batch file 2 (is supposed to empty the subfolders, then move along to the next folder in the list):
#echo off
set _Uname=%1
for /f "eol=: delims=" %%F in ('dir /b^|find "%_Uname%"') do <NEXT BIT I CAN'T FIGURE OUT>
The problem I'm having is getting the command to pull within the main folder.
I know this code works when I throw it right into DOS:
for /r %f in (*) do #move /y "%f"
But I can't have a for, do, for, do and I don't want to have to type that in for the 5000 or so folders I'd like to remove duplication from.
Thanks for any help!
Tyler
Is there a reason for the 1,3? You aren't using the 3 (%%H) in your examples.
How about this? Just change to each directory and back, before your 2nd command.
#echo off
FOR /F "tokens=1,3 delims=," %%G IN (Pt1Test.csv) DO (
pushd *%%G*
for /r %%a in (*) do #move /y "%%a"
popd
)

Rename A Group Of Files

I need to rename a group of files in the same folder.
When I try to run the batch file, it doesn't work correctly:
`ren *.txt Updated_*.txt`
The file names contain date_names_location.txt, examples are below
08232013_name1_nyc.txt
08212013_name1_nyc.txt
08232013_name1_la.txt
08212013_name1_la.txt
When I run the batch file I get back:
Updated_1_name1_nyc.txt
instead of
'Updated_08232013_name1_nyc.txt'
Any ideas on how to fix? Thanks
This is one way:
#echo off
for /f "delims=" %%a in ('dir /b /a-d *.txt') do ren "%%a" "Updated_%%a"
REN has no insert mode, so it just replaces the beginning of your file names. Try the solution provided here
Batch renaming files using Windows 7 REN (adding prefix)?

Copy a file into multiple folders, not subfolders batch

Looking for a batch file that would copy a file into multiple folders (within the same directory that the batch file was placed), but not their subfolders.
For example:
I need K:\NewCustomers\NewPartNumber.Bat to go into K:\NewCustomers\Customer Name\
but not any subfolder of \Customer Name\, there being 200-300 "Customer Name" folders.
I was using:
for /R "K:\NewCustomers\" %%a in (.) do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
But this is recursive, and now that there are folders inside of these other folders, I can't run this command without putting it in every subfolder.
I tried running a for /d loop:
for /d "K:\NewCustomers\" %%a in (.) do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
but was unsuccessful at the syntax and after a while now of looking some things up and trying different things, I'm trying to pull my hair out looking for an answer. I get this error:
K:\NewCustomers* was unexpected at this time.
Using for /d to loop through folders in a non-recursive fashion indeed the right way to go, but you need to use it like this:
for /d %%a in ("K:\NewCustomers\*") do copy "K:\NewCustomers\NewPartNumber.bat" "%%a"
Alternatively, you can use a for /f loop in combination with dir:
#echo off
pushd "K:\NewCustomers"
for /f "tokens=*" %%a in ('dir /A:D /B') do copy "NewPartNumber.bat" "%%a"
popd
Personally, I prefer the first method more, though.

Resources