I have files in one folder with the names like
John-Smith-014_15616812_5165
Robert-Walker-014_15644812_5145
I want these files to be checked and if they matched to the already existed one then automatically moved there.
Destination folders are already created and they look like:
John-Smith-156.81.251.452
Robert-Walker-12.108.36.5418
I have tried this code:
#echo off
setlocal EnableDelayedExpansion
pushd "C:\dest"
FOR %%G IN (*) DO (
FOR /F "tokens=1 delims=_" %%a IN ("%%G") do (
set "outFolder=%%a"
for /D %%i in (*.*) do (
for /F "tokens=1 delims=_" %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
move "%%G" "!outfolder!"
)
)
popd
pause
This code partly works but it can't make difference between Robert-Walker to Robert-Brown and it will move both of them to the folder with the name Robert-Walker-12.108.36.5418
In this code it won't check both first and second tokens in order to sort those files according to their name and surname, instead it sorts only by their name.
Related
I am working on writing a script that will create folders based on parts of file names. The problem is the Delim's are both the same. File names look like this: "Bacon-HunterBacon-00002" I am looking at using the first and second tokens to be the folder name. So the folder would be named Bacon-HunterBacon
#echo off
setlocal enabledelayedexpansion
for %%A in (*.cr2 *.jpg) do (
echo file found %%A
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
for /f "tokens=1,2* delims=-" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%A" "!folname!"
)
echo Finished
In the loop for /f"tokens=1,2* delims=-" %%D in ..., the first token is assigned to %%D, the second one to %%E and the rest (*) to %%F (but which you do not use), so you should do set folname=%%D-%%E to get the desired folder name in variable folname:
#echo off
setlocal EnableDelayedExpansion
for %%A in ("*-*-*.cr2" "*-*-*.jpg") do (
echo file found %%A
for /f "tokens=1,2 delims=-" %%D in ("%%~nA") do set "folname=%%D-%%E"
echo folder name !folname!
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
echo Moving file %%A to folder !folname!
move "%%~A" "!folname!"
)
endlocal
echo Finished
The loops for /f "delims=" %%B and for /f "delims=" %%C are useless, you could use %%~nA and %%~xA directly to get the values fname and fextn, respectively. But you do not need these interim variables at all, therefore I removed them.
And you should use the quoted set syntax like set "folname=%%D-%%E" in general.
I also changed the file masks to not match files that contain less than two hyphens (-).
Actually you do not even need the interim variable folname when you use the tokens %%D and %%E immediately. So you do no longer need delayed expansion any more:
#echo off
setlocal
for %%A in ("*-*-*.cr2" "*-*-*.jpg") do (
echo file found %%A
for /f "tokens=1,2 delims=-" %%D in ("%%~nA") do (
echo folder name %%D-%%E
if not exist "%%D-%%E" (
echo Folder %%D-%%E does not exist, creating
md "%%D-%%E"
) else (
echo Folder %%D-%%E exists
)
echo Moving file %%A to folder %%D-%%E
move "%%~A" "%%D-%%E"
)
)
endlocal
echo Finished
I'm trying to move files into existing sub-folders based on the file names.
For example, I want to move a file named AP16742 found in the directory X:\Files into a folder named AP in the directory X:\Files\AP. Other files named MO14823 I want to move into a folder named MO in the directory X:\Files\MO.
I'm inexperienced in coding, so I need explanations to go with a provided example.
This is what I tried:
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN ('dir /b /a-d "%sourcedir%*.xml" ') DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN ( 'dir /b /ad "%destdir%*%%b*" ' ) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
A simple explanation would have been:
you want to distribute files in X:\Files to subfolders with the
first 2 letters of the file name.
To get a substring you need to set the content from for variable into
a normal variable.
Setting and using a variable inside a (code block) requires delayed
expansion.
#Echo off&SetLocal EnableExtensions EnableDelayedExpansion
Pushd "X:\Files"
For %%A in (*) do (
Set "File=%%~nA"
if not exist "!File:~0,2!" md "!File:~0,2!" 2>&1>Nul
Move "%%A" "!File:~0,2!"
)
Popd
In case you want to move only distinct 2 letter pairs and not all files that's also possible without great effort.
A modification of LotPings answer, this uses RoboCopy with its /MOV option, which will create directories as needed and move the files to them:
#Echo Off & SetLocal EnableDelayedExpansion
CD /D "X:\Files" 2>Nul || Exit /B
For %%A In (*) Do (Set "File=%%~nA"
RoboCopy . "!File:~,2!" "%%A" /MOV >Nul)
Currently I am using this batch script and it loops through all subfolders, but I don't want that. I want it to only loop through the main folder and not subfolder.
%1 is the folder path for file search
set SIGNTOOL="%~dp0Signtool.exe"
set PFXFILE="%~dp0Temporary_Signing.pfx"
set PASSWORD=12345
SET FILES="%~dp0Signing_Files_List.txt"
set TIMESTAMP="http://timestamp.verisign.com/scripts/timestamp.dll"
set "FILESPATH=%1"
FOR /F "delims= " %%a IN ('type %FILES%') DO (
FOR %FILESPATH% %%f IN (%%a) DO (
%SIGNTOOL% sign /f %PFXFILE% /P %PASSWORD% /t %TIMESTAMP% %%f
)
)
Gives error at FOR %FILESPATH% %%f IN (%%a) DO
%FILESPATH% was unexpected at this time.
Since you've omitted to include data samples, it's not clear what you are trying to do.
FOR %FILESPATH% %%f IN (%%a) DO (
You appear to have modified a for /r here since you are referring to subdirectories.
Perhaps
FOR %%f IN (%FILESPATH%\%%a) DO (
will scan yor files. you will probably need to use %FILESPATH%\%%f instead of %%f in your sign line.
in my company we create software for different customers to handle our machines. As each product is unique, so is the control software, but not completely new. So for a start we copy an old project, rename it and change it until it fits.
Usually the directory name is the name for the new program (our ide uses the directory name, but also relies on some other files following the same name scheme).
For the renaming I've wrote a short batch script which finds the old name scheme and retrieves from the directory name the new one.
But the only solution I've found for this uses a new batchfile for each file to be renamed.
Is there a better way to get the content of !progNeu! ?
#echo off
SETLOCAL enabledelayedexpansion
set pfad=%CD%
for /d %%A in (%pfad%) do (set progNeu=%%~nxA)
for /f "tokens=1,2 delims=|" %%B in ('dir /b *.s19 ^| findstr /v "appl"') > do (
set progAlt=%%B
set rumpfAlt=!progAlt:.s19=!
>x ECHO !rumpfAlt!&FOR %%C IN (x) DO SET /A strlength=%%~zC - 2&del x
for %%D in (!rumpfAlt!*.*) do (
set progAlt=%%D
>x.bat echo #echo off
>>x.bat echo set ausg=!progAlt!
>>x.bat echo echo %%ausg:~!strlength!%%
for /f "" %%E in ('x.bat') do (
set "dateiNeu=!progNeu!%%E"
if exist !dateiNeu! del !dateiNeu!
rename %%D !dateiNeu!
)
del x.bat
)
)
If I have not missed something, this could be the equivalent to your code
#echo off
setlocal enableextensions disabledelayedexpansion
set "pfad=%CD%"
for /d %%A in ("%pfad%") do (
for /f "delims=" %%B in ('
dir /b *.s19 ^| findstr /v "appl"
') do for %%D in ("%%~nB*.*") do (
set "progAlt=%%D"
setlocal enabledelayedexpansion
for %%E in ("!progAlt:%%~nB=!") do (
endlocal
echo move /y "%%D" "%%~nxA%%~E"
)
)
)
I have removed almost all the inner variables that are simply using the values that the for replaceable parameters already hold, and used the adecuated modifiers to retrieve the needed part from the file names.
So I have this Bat file I was able to get working today. I need the files to be renamed before they are moved but for some reason it cannot find the file when I add Ren "%%G" before the Copy.
Here is my code, I hope someone will be able to help me.
#echo off
setlocal EnableDelayedExpansion
pushd "E:\Paqtrack\Download\PDF\"
FOR %%G IN (*.pdf) DO (
FOR /F "tokens=1 delims=_" %%a IN ("%%G") do (
set "outFolder=\\appauto1\LazarusAttachments\%%a"
for /D %%i in (*.*) do (
for /F "tokens=1 delims=_" %%b IN ("%%i") do (
if "%%a"=="%%b" set "outFolder=%%i"
)
)
if not exist "!outfolder!" md "!outfolder!"
copy "%%G" "!outfolder!"
)
)
popd
Instead of using ren, try just setting a name variable, and then adding it to the end of the outfolder.
For example:
set "name=foo.bar"
copy "%%G" "!outfolder!/%name%"