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.
Related
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.
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
)
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. ;)
I'm trying to create a standardised directory structure to prepare a drive ready for tidying and migration to a new location. What I'm looking to do is run a batch file in the root of the drive which works through all subdirectories and creates a new folder called 'Archive' in each of them ready for files to be tidied up and later moved or deleted.
I'm trying to use FOR /F with an MD command to make the folders, but I'm really struggling to understand the syntax re. tokens and variables. I'm trying to adapt from something like
for /f "tokens=*" %a in ('dir /b /s /a-d') do #copy "%a" "c:\Single-Folder"
which I nabbed from over here replacing #copy with #md... but I'm really out of my depth.
Any offers of help much appreciated
The command you are using:
dir /b /s /a-d
lists everything except directories (the - sign negates the attribute). So the right command you have to use is this:
dir /b /s /ad
(try it yourself!).
FOR /F reads the output of the dir command, one line of text at a time. tokens=* will get the whole line (removing leading spaces). I think you need this:
for /f "tokens=*" %a in ('dir /b /s /ad') do #echo mkdir "%a\Archive"
when you are sure it is right, remove the echo command to actually execute the mkdir.
I think what you want to do is this type of structure:
#Echo OFF
REM By Elektro H#cker
For /D /R "%SYSTEMDRIVE%\" %%# in (*) do (MKDIR "%SYSTEMDRIVE%\Single-Folder\%%~p#")
Pause&Exit
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