I put a for loop as below to my PC desktop for example, in order to move all files to a corresponding folder named by its extension.
for %%G in (*.*) do (
md "%%~xG" 2>nul
move "%%G" "%%~xG"
)
So far the script is ok just I want to improve it with the followings,
make folder only if it is not exist
make folder by parameter expansion will create a folder .jpg, but what i wanted is jpg, can I use something like set x=%%~xG, with setlocal and EnabledDelayExpansion, and then !x:~1! to trim the dot
Don't move the batch file itself to the bat folder created. Shall I use expansion like %f0 ?
Any help would be appreciated.
setlocal enabledelayedexpansion
for %%a in (*.*) do (
if not "%%~xa"=="" (
set "ext=%%~xa" & set "ext=!ext:~1!"
if not exist "!ext!" mkdir "!ext!" 2>nul
if not "%%~fa"=="%~f0" (
move "%%a" "!ext!\%%a" >nul
)
)
)
endlocal
While writing, I realised #Stephan 's comment is identical to this answer, so precedence to him!
Related
I am in the middle of batch extracting screenshots for contents we are planning to use on a tube site I am working on.
The jpeg files per content is labled as followed:
6c82c0239f6eb839-1
6c82c0239f6eb839-2
all the way to 120
The file name is different per content
a82384e2c46ba4af-1
a82384e2c46ba4af-2
etc.
They will all be extracted to a singe folder.
So I basically need a batch file that will create folders based on the content name without the dash and number and move all 120 jpegs in the folder with the content name.
For example:
Create folder named 6c82c0239f6eb839 and
move 6c82c0239f6eb839-1 to 6c82c0239f6eb839-120 in to the created folder.
I saw another thread with the following batch file. its pretty much what I want but the folder name is only 3 characters long and the files are copied to the newly created folders instead of moving them.
#echo off
SetLocal EnableDelayedExpansion
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
set Folder=!Name:~0,3!
xcopy /y "%%a" !Folder!\
)
Could someone change this so that it will display full file name without the dash and number for the folders and move files in its respective folders instead of copy?
Thank you
#echo off
setlocal
#rem Get each jpeg file.
for /F "delims=" %%A in ('2^>nul dir /b *.jpeg') do (
rem Get filename as token before the dash.
for /f "delims=-" %%B in ("%%~A") do (
rem Make dir if needed.
if not exist "%%~B" md "%%~B"
rem Check if isdir.
2>nul pushd "%%~B" && popd
if errorlevel 1 (
>&2 echo Failed isdir "%%~B".
) else (
rem Do the move operation.
>nul move /y "%%~A" "%%~B"
if errorlevel 1 (
>&2 echo Failed move "%%~A" to "%%~B"
)
)
)
)
exit /b %errorlevel%
The code is well remarked so if you want to understand
the evaluated code by changing #echo off to #echo on.
The use of %errorlevel% after the exit /b is not
required though will let you know what the errorlevel is
when #echo on is used.
The pushd tests for a directory
(even if it is a symlink).
errorlevel is checked to decide if to echo a
error message or do the move.
As the for loop variables are used direct, use of
enabledelayedexpansion is not needed.
Many commands support the argument of /? to get help
about the command. i.e. move /?.
If you only try to copy the correct jpeg to the correct folder, you can do this:
#echo off
SetLocal EnableDelayedExpansion
CD <CORRECT ROOT PATH>
for /F "delims=" %%a in ('dir /b *.jpeg') do (
set Name=%%a
REM I presume all the files have 16 characters before the dash
set Folder=!Name:~0,16!
IF NOT EXIST !Folder! MKDIR !FOLDER!
xcopy /y "%%a" !Folder!\
)
I was not able to test.
First of all, I would like to apologize for my manners regarding my initial post.
The answer by micheal_heath has resolved my issue.
Furthermore, I happened to find this post by user Salmon Trout from a different site which also worked.
Batch file to make folders with part of file name and then copy files
#echo off
setlocal enabledelayedexpansion
for %%A in (*.psd *.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* 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
pause
I just changed the the following line remove the hypen and numbers to create folders for the file name properly.
for /f "tokens=1* delims=-***" %%D in ("!fname!") do set folname=%%D
I still lack the knowledge on why and how both methods work, but this has been an interesting start for me. I hope other beginners trying to solve a similar issue can find something useful from this post.
I have a list of filenames without their extension and a folder and sub-folders of those files with their extensions. I am trying to use the list to copy those files to a different location. I tried to use a script I found here and modified it:
#echo off
FOR /R "P:\Case\MyCase\Productions" %%G in (.) do (
for /F "delims=" %%i in (UniqueFileList.txt) do (
if exist %%G\%%i.* xcopy %%G\%%i.* "C:\Temp\CopiedFiles" /D /Y
)
)
My file names are TIN00001.msg, TIN00002.txt, TIN00003.jpg, etc...
and the names in my file list is TIN00001, TIN00002, TIN00003, etc...
How can I use a script to copy the files ?
Any help is appreciated.
Thanks !!
Mustu
I'm not sure if I fully understand your intention but my best guess is that something like this would suit you.
#Echo Off
Set "rootDir=P:\Case\MyCase\Productions"
Set "destDir=C:\Temp\CopiedFiles"
Set "listTxt=%~dp0UniqueFileList.txt"
For /F "Delims=" %%A In ('Where/R "%rootDir%" *.*^|FindStr/LIG:"%listTxt%"'
) Do FindStr/LIX "%%~nA" "%listTxt%">Nul 2>&1 && XCopy "%%A" "%dstDir%" /D /Y
If %listTxt% isn't in the same location as the script then change it's location to a full path too.
I am trying to move differents files into specific folder through a loop.
For each file, I set modalite, serie_no and serie_name and want to create a specific folder based on those attributes. The file will be also moved to this folder.
But the IF EXIST command fail (The syntax is not correct).
#echo on
setlocal enabledelayedexpansion
set "arg1=%~1"
cd /d D:\!arg1!
for /r %%i in (*.*) do (
REM ...
REM stuff to set modalite, serie_no and serie_name
REM ...
if exist D:\!arg1!\!modalite!(
cd D:\!arg1!\!modalite!
) else (
mkdir D:\!arg1!\!modalite!
cd D:\!arg1!\!modalite!
)
if exist D:\!arg1!\!modalite!\!serie_no!_!serie_name!(
move %%i D:\!arg1!\!modalite!\!serie_no!_!serie_name!
) else (
mkdir D:\!arg1!\!modalite!\!serie_no!_!serie_name!
move %%i D:\!arg1!\!modalite!\!serie_no!_!serie_name!
)
)
I tried to use "modalite","serie_no" and "serie_name" but still have the error
Can I use the if exist command in a for loop in that way? Or should I use for /f %%a in () do ()?
Or maybe there is another way to proceed? I am open to everything.
I have several folders that have files with double file extensions along with regular file extensions. I need to create a batch script to search all the folders and remove the last extension with any files that have double extensions. None of the file extensions are consistent.
Here's an example
C:\test\regular.exe
C:\test\picture.jpg.doc
C:\newtest\document.doc.pdf
End Result I need
C:\test\regular.exe
C:\test\picture.jpg
C:\newtest\document.doc
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF NOT EXIST "%%~dpni" ECHO REN "%%~fi" "%%~ni"
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" ECHO CAN NOT REN "%%~fi" "%%~ni"
)
GOTO :EOF
This batch should accomplish the task.
For each file in the tree rooted at sourcedir, if the NAME of the file itself contains an 'extension' and the filename without the original extension does not exist, then rename the file. That way, if ...picture.jpg.doc is found, the rename should occur only if ...picture.jpg does not exist.
The command to rename is simply ECHOed. You'd need to remove the ECHO keyword to activate the rename - after verifying that's what you want to do.
I've added a second line to report that a rename could not be done because of an existing file.. This could be done very slightly better, but it will work.
Revised to modify name in case simple rename can not be done.
Caution - this version will rename immediately - there are no ECHOes to provide a list first because it's nonsense to provide such a list when renaming a file may produce different results on the main rename run.
#ECHO OFF
SETLOCAL
SET sourcedir=c:\sourcedir
FOR /r "%sourcedir%" %%i IN (*.*) DO (
FOR %%n IN ("%%~ni") DO IF NOT "%%~xn"=="" IF EXIST "%%~dpni" (
SET renreq=Y
FOR %%a IN (new alt extra another 1 2 3 4 5 6 7 8 9) DO IF DEFINED renreq (
IF NOT EXIST "%%~dpi%%~nn_%%a%%~xn" (
REN "%%~fi" "%%~nn_%%a%%~xn"
SET "renreq="
)
)
IF DEFINED renreq ECHO CAN NOT REN "%%~fi"
) ELSE (
REN "%%~fi" "%%~ni"
)
)
GOTO :EOF
Reasonably obviously, the list of "extras" can be extended if required.
Try this and remove the echo, if the output is OK:
#echo off &setlocal
for /r \ %%i in (*) do (
for %%j in ("%%~ni") do if "%%~xj" neq "" echo ren "%%~fi" "%%~nj"
)
Edit: added support for the entire HD.
I am putting together a script for my media server. I need to run through all the
files in a directory and then ultimately create softlinks in another folder to act as a video playlist. I'm just starting out and I'm already having problems with my batch script
#echo off
SetLocal EnableDelayedExpansion
set TV="G:\TV"
FOR /R %TV% %%G in (.) DO (
Pushd %%G
Echo now in %%G
for /f %%f IN ("dir /b") do (
Echo %%f
)
Popd )
Echo "back home"
)
This produces a listing for each file but the filenames cut out after any spaces! It looks something like this:
now in G:\TV\UCB\UCB Season 3\.
Upright
Upright
Upright
Upright
...
now in G:\TV\Venture Bros\Season 3\.
File Not Found
now in G:\TV\Venture Bros\Season 4\.
The.Venture.Bros.S04E03.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E04.HDTV.XviD-2HD.avi
The.Venture.Bros.S04E05.HDTV.XviD-2HD.avi
What can I do to fix this? Once I have the file it should be easy to create a soft link.
Just add quotes around your file names:
Pushd "%%G"
you may replace all your BAT with a simple oneliner FOR loop.
FOR /R G:\TV %%f in (*) do echo create soft link for %%f