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. ;)
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.
Similiar problem as previously solved question here - Batch create folders based on part of file name and move files into that folder
QUESTION: I have 15k files that I want to extract the first word from file name to create a folder, then move all those respective files into the folder.
This is driving me crazy trying to figure out!!!
**EXAMPLE: **Using the files below, I want all files beginning with PTC to move into a new folder called PTC, then the same process with files beginning with PRIORITY.
PTC-Affiliate Digital Asset Refresh (160x600)
PTC-Affiliate Digital Asset Refresh (160x600)2
PTC-Affiliate Digital Asset Refresh (160x600)3
PRIORITY-Affiliate Digital Asset Refresh (160x600)
PRIORITY-Affiliate Digital Asset Refresh (160x600)2
PRIORITY-Affiliate Digital Asset Refresh (160x600)3
I tried revising existing code (below), but am running into 2 issues:
File names use spaces, not underscore - how do I denote that in the code?
When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.
CODE I AM TRYING TO EDIT TO DO WHAT I WANT:
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\[directory\]"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=-" %%a IN ('dir /b /a-d "*\_*_\*_*\_*"') DO (
MD %%a 2\>nul
MOVE "%%a\_%%b\_%%c\_%%d\_%%e" .%%a\\ 2\>nul
)
POPD
GOTO :EOF
Any help or suggestions would be VERY welcomed.
Thank you!
I tried revising existing code (below), but am running into 2 issues:
File names use spaces, not underscore - how do I denote that in the code?
When I rename a test file using underscores instead of spaces, the code below will create the folder, but not move the actual files.
I am not able to solve for either of these issues.
Maybe your approach of focusing on old code is not allowing you to focus on the task at hand :)
You really only care about the first word, delimited by -. So split only that, then move all files starting with the first word once off:
#echo off
SET "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "delims=-" %%i in ('dir /b /a-d "*-*"') do (
mkdir "%%~i">nul 2>&1
move %%i* "%%~i">nul 2>&1
)
popd
however, there is a chance (though you did not specify this) that there are other files that you might want to skip in the directory, then might do:
#echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for %%a in ("*-*(*x*)*") do for /f "delims=-" %%i in ('dir /b /a-d "%%~a"') do mkdir "%%~i">nul 2>&1 && move "%%a" "%%~i"
popd
and if you feel like some regex and be more specific with your query, then findstr will help by enforcing wildcards, spaces and/or alphabetical or numeric character ranges as in the extract from below. findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$" :
#echo off
set "sourcedir=C:\[directory\]"
pushd "%sourcedir%"
for /f "tokens=1,*delims=-" %%a in ('dir /b /a-d ^| findstr /R /C:"[a-z]-*[ ]*[ ]*[ ]*[ ]([[0-9]*[0-9]x[0-9]*[0-9])[0-9]*[0-9]$"') do (
mkdir "%%~a">nul 2>&1
move "%%a-%%b" "%%~a"
)
popd
I have a relatively simple task to do. I have to move 100 files which are located in various different folders, into one single folder. So far I have the script that will do this if i am moving the files from one folder to another folder based on the list of files.
#echo off
set Source=C:\IMAGES_SOURCE
set Target=C:\IMAGE_DESTINATION
set FileList=C:\ImageList.txt
echo.
if not exist "%Source%" echo Source folder "%Source%" not found & goto Exit
if not exist "%FileList%" echo File list "%FileList%" not found & goto Exit
if not exist "%Target%" md "%Target%"
for /F "delims=" %%a in ('type "%FileList%"') do copy "%Source%\%%a"
"%Target%"
:Exit
echo.
echo press the Space Bar to close this window.
pause > nul
So for this instance, I created the list of the items and scrip will look for the file name in source location and once find them it will move them to a destination location.
I need to adjust this so it reads the names of the files and try to find them in the list of the folders that they might be.
What i have tried is to change the set Source to be also separate list that will contain posible folder names:
set Source=C:\DirList.txt
But when i do this it does not copy the images.
Any idea how to do this?
First thing that comes to mind is dir /S meaning it will search using the dir command. This is untested, but you'll get the idea:
#echo off
cd /d C:\Images_source
setlocal enabledelayedexpansion
for /F "delims=" %%i in (C:\Imagelist.txt) do (
set var="%%i"
for /F "delims=" %%a in ('dir /B /S !var!') do echo copy "%%~fa" C:\Image_Destination
)
For more on the commands I used, try help by running from from commandline for /? and dir /?
Once confident that it will copy only what you want to copy/move, then remove the echo before copy.
I need help with a script that first finds all files in a directory with a certain string, then uses the filenames in a variable to be used in a script.
So:
Find files and filenames
Saves file?
Start some kind of loop? that changes a variable then executes the
belonging script
Repeat till all filenames have been used.
My code here..
#Echo off
For /r C:\work %%G In (*) Do #Findstr /M /S "string" > filenames.txt %%G
Set Var1=0
For %%G In (*) Do (
Var1=<filenames.txt (???)
script
script
I haven't writen "script" myself and friend help me with it, if you would like to see it do you need to wait until I can get to my other computer at home.
Thanks on beforehand!
Find files and filenames
Saves file
set "search=what I want to find"
(for /f "delims=" %%a in ('dir /a-d /b /s "C:\work" ^| findstr "%search%"') do echo (%%~fa)>filenames.txt
Start some kind of loop? that changes a variable then executes the belonging script
Repeat till all filenames have been used.
for /f "delims=" %%a in (filenames.txt) do (
REM here do something inside the loop
REM until all file names from filenames.txt were processed
)
This is designed to find files in c:\work that match a string, and echo the filenames.
#echo off
cd /d "c:\work"
for %%a in ("*string*") do (
echo "%%a"
)
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.