Batch empty subfolders from folders in a CSV file - batch-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
)

Related

Moving files with a batch file

I have 30 folders, one for each travel group with 10 subfolders each, each subfolder corresponds to an activity.Some pictures dont match with the group, so i have a list of the pictures that dont match.
Im using this batch to move the jpgs from the folders but it's putting them all together. is there any way that the batch creates a folder with the name of the folder it's been moved from?
for /r "originfolder" %%# in (*) do findstr "%%~nx#" "filelist.txt"&&move "%%#" "destinyfolder"
PAUSE
You'll need a few more lines. There are a few different ways, this being one of them.
#echo off
setlocal enabledelayedexpansion
pushd "originfolder"
for /F "usebackq delims=" %%a in ("filelist.txt") do for /F "delims=" %%i in ('dir /b /s "%%a" 2^>nul') do (
set "filepath=%%~dpi"
set "filepath=!filepath:~0,-1!"
for %%f in (!filepath!) do set "destpath=%%~nxf"
mkdir "C:\Full path to\destinypath\!destpath!"
move "%%~a" "C:\Full path to\destinypath\!destpath!"
)
popd
simply get the path of the file, then use the last name of the path (folder where file exists) and create that folder name in your destination path, before moving the file.
Note, this is untested code, so please use a test scenario before attempting this in production.

Automatically Create Folders Based on String in Filename

I need a batch file to create a process for a large list (2k) of TIF files in a local folder directory.
The filename structure is, for example: 12345_1.tif.
I need the batch to:
1 - Create a folder name based on the number(s) after the underscore, as this is the only constant in the naming. The folders only based on this sole number.
2 - Copy and move the file into the newly created folder.
In the example above, the batch would create a folder called 1 and then move the file 12345_1.tif into that folder. If it found another file such as 54321_1.tif, that file would also be moved to the "1" folder. In my files, the numbers after the _ range from 1 through 77, and there could multiple files that share the same number after the _.
I've observed some similar scripts online, but I need help to modify my requirement. Is it possible to modify this to meet my requirement?
#echo off &setlocal
for /f "delims=" %%i in ('dir /b /a-d *.PDF') do (
set "filename1=%%~i"
setlocal enabledelayedexpansion
set "folder1=!filename1:~11,6!"
mkdir "!folder1!" 2>nul
move "!filename1!" "!folder1!"
endlocal
)
It seems you didn't understood my comment, so I post it here as an answer with complete code:
#echo off
for /F "tokens=1,2 delims=_." %%a in ('dir /B *.tif') do (
md "%%b" 2>NUL
move "%%a_%%b.tif" "%%b"
)

Proper file locations

We have been having some issues where people have been putting files into the folder for the wrong job number. files for 664585_custnum_qty_filetype.dat should be in folder 664585 I am looking for a way to make sure that the files in a given folder all start with the foldername. I have been working with.
#ECHO OFF
FOR /R "V:\Work" %%G in (.) DO (
Pushd %%G
REM Echo now in %%G
for %%a in (.) do set currentfolder=%%~na
echo %currentfolder%
REM this is where I would be finding files that dont start the right way.
Popd )
Echo "back home"
Pause
trying to combine two other things that I have seen for iterating through folders and finding current directory name
Ultimately I want to list all the files that are out of place so that I can find them and figure out why they're not/who put them there. So I would like to have a program that iterated through all of the folders in V:\work into folders like 665485 and 332185 CustName displaying any files which do not start with 665485 or 332185 respectively.
I do have cygwin on my system, though not enabled all the time, and was considering ls, referencing C:\cygwin\bin\ls.exe directly to avoid it throwing off the normal functionality of some MS commands with the same name.
Any suggestions on how to get my list of files that dont start with the number from the foldername?
#echo off
for /d %%i in (*) do (if "%%i" NEQ "xArchive" (for /f %%j in ("%%i") do (dir "%%i" /b|for /f %%k in ('find "%%j" /v') do #dir "%%k" /b /s|find "thumbs.db" /v /i|find "xArchive" /v /i))
put this in the directory that contains the numbered folders and run it there. I'll be back in a few hours If you need me to change it. ;)

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.

Batch Script - Empty Multiple Directories In Paths With A Pattern

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

Resources