I got a bunch of pictures taken with my camera in a parent folder. The filenames has this format; 'yyyymmdd_ttmmss.jpg', eg '20151008_0730.jpg'.
I want to create folders based on the 'yyyymmd'-part of the filename, but with the format 'yyyy-mm-dd'. So the file '20151008_0730.jpg' is moved into a folder named '2015-10-08'. This is what I got so far:
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\temp"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*.jpg"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
But I don't know how to format the %%a-variable before creating the folder.
(This is a similar question asked before, but not with creating the folder on this format)
#echo off
setlocal
copy nul "%tmp%\20151008_0730.jpg"
copy nul "%tmp%\20151009_0731.jpg"
copy nul "%tmp%\20151010_0732.jpg"
set "sourcedir=%tmp%"
pushd %tmp%
for /f "delims=" %%a in ('dir /b /a-d "*_*.jpg"') do (
set "file=%%a"
setlocal enabledelayedexpansion
set "folder=!file:~0,4!-!file:~4,2!-!file:~6,2!"
echo:
rem check if folder already exist
if not exist "!folder!\nul" echo md "!folder!"
echo move "!file!" "!folder!"
endlocal
)
popd
exit /b 0
output:
md "2015-10-08"
move "20151008_0730.jpg" "2015-10-08"
md "2015-10-09"
move "20151009_0731.jpg" "2015-10-09"
md "2015-10-10"
move "20151010_0732.jpg" "2015-10-10"
You have to clean up this sample by changing sourcedir and pushd, removing echo in front of each commands, also copy nul "....".
I used here VarSubstring and delayedexpansion
Related
so I have a bunch of folders with similar directories to the below:
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png
I want to rename all the .png files the number just before the first randomStringofCharacters. So basically
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
Is there any batch script that can do this? I have tried:
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("*.png") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&Exit
Yet this only renames the file with the parent directory.
And if possible is there a way to move all such renamed .png files in to a newly made folder in .\page\ (the same place where the .bat is) with the folder name of page?
Thanks in advance!
Could be something like this (you have to drag and drop the main folder to the batch):
#echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
You should not use for /R to handle items in a predefined directory hierarchy depth:
#echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
Note, that this approach does not check whether there is only one .png file at each location.
I have a folder containing several hundred sub-folders in the format Name, ID. Each of these folders contain several sub folders, some of which contain spaces in their names. I would like to rename only the sub folders and not the parent folders by replacing the spaces with underscores, e.g. C:\Location\John, 1234\My Documents to C:\Location\John, 1234\My_Documents.
I have tried modifying a piece of script I have found on here but it changes the parent folder as well
Here is the unedited code:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "StartFolder=C:\Tydelik"
cd /D %SystemRoot%
set "RenameError="
rem Rename all folders containing at least one space character in folder name.
for /F "delims=" %%I in ('dir "%StartFolder%\* *" /AD /B /S 2^>nul') do call :RenameFolder "%%I"
if defined RenameError echo/& pause
rem Restore initial environment and exit this batch file.
endlocal
goto :EOF
:RenameFolder
set "NewFolderName=%~nx1"
set "NewFolderName=%NewFolderName: =_%"
set "FolderPath=%~dp1"
if not exist "%FolderPath%" set "FolderPath=%FolderPath: =_%"
set "FullFolderName=%FolderPath%%~nx1"
if not exist "%FullFolderName%\" set "RenameError=1" & goto :EOF
for %%J in ("%FullFolderName%") do set "FolderAttributes=%%~aJ"
if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe -h "%FullFolderName%"
ren "%FullFolderName%" "%NewFolderName%" 2>nul
if errorlevel 1 goto ErrorFolderRename
if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h "%FolderPath%%NewFolderName%"
goto :EOF
:ErrorFolderRename
echo Error renaming folder "%FullFolderName%"
set "RenameError=1"
if "%FolderAttributes:~3,1%" == "h" %SystemRoot%\System32\attrib.exe +h "%FullFolderName%"
goto :EOF
As I said the expected output for each sub folder should be C:\Location\John, 1234\My_Documents instead of C:\Location\John, 1234\My Documents. Currently with the code I have, I get C:\Tydelik\John,_1234\My_Documents.
While Compo's solution renames folders "depth=2", this renames just the "leafes" (very last folders of a tree, "depth=last"). I kept your call approach to avoid delayed expansion and resulting possible problems (folder names with ! - unlikely in your situation, but one never knows...)
#echo off
setlocal
set "sourcedir=..\..\"
for /f "delims=" %%I in ('dir "%sourcedir%" /ad /b /s 2^>nul') do call :RenameFolder "%%I"
goto :eof
:RenameFolder
dir /ad /b /s "%~1" 2>nul | find /v "" >nul && goto :eof ::skip renaming, if a subfolder exists
set "leaf=%~nx1"
ECHO ren "%~1" "%leaf: =_%"
goto :eof
Note: for security reasons I disabled the ren command by just echoing it. If it works as intended, remove the ECHO.
Here's an example of what I think you're looking for, based upon the fact that you're interested only in renaming subdirectories of "C:\Tydelik\Name, ID", not any contained within those subdirectories:
#Echo Off
SetLocal DisableDelayedExpansion
Set "SourceDir=C:\Tydelik"
For /F "EOL=?Delims=" %%A In ('Dir /B/AD "%SourceDir%" 2^>NUL'
)Do Set "TargetDir="&For /F "EOL=?Delims=" %%B In (
'Dir /B/AD "%SourceDir%\%%A" 2^>NUL') Do (Set "TargetDir=%%B"
SetLocal EnableDelayedExpansion
If Not "!TargetDir: =!"=="!TargetDir!" (
Ren "%SourceDir%\%%A\%%B" "!TargetDir: =_!")
EndLocal)
I have a .txt file with a list of files that are on a network drive (M:). I need a batch file to go through that list, search for the files which are in sub-directories of a single folder on that drive, and then copy them to another folder. I have tried quite a few solutions with no luck.
the text file is a list of files and extensions i.e.
abc.step
afer.iges
ca76dc7d.sldprt
Here's what I tried so far
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
cls
set dest=C:\Users\kduquette.000\Desktop\Files
for /f "TOKENS=*" %%f in (C:Desktop\Files\list.txt) do (
set i=1
for /f "tokens=*" %%F IN ('dir M: "%%~f"') Do (
for %%N in ("%%F") Do (
set name=%%~NN
set ext=%%~XN
)
copy "%%~F" "%dest%\!name!_!i!!ext!"
set /A i=!i!+1
)
)
ENDLOCAL
We can just do a check if file exists and copy it:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do if exist "M:\Cads\%%i" echo copy "M:\Cads\%%i" "%dest%"
Note, for now, I added echo to the copy string, so you can test the result first. Once happy with the results, remove echo
If however you want to search for the files, recursively throughout M:\ drive then:
#echo off
set "dest=C:\Users\kduquette.000\Desktop\Files"
for /f "delims=" %%i in (C:\Desktop\Files\list.txt) do (
pushd M:\
for /f "delims=" %%a in ('dir /b /s "%%i"') do echo copy "%%~fa" "%dest%"
popd
)
I have a bunch of files which have names like the following:
BS01_1234_1234.jpg
BS01_1235_6789.jpg
TP01_1234_6879.jpg
All kept in a local C drive folder.
I'm hoping to move these to a networked folder \\autrsrv02\Tempdata\Justine Tennent\movetofolder but grouped into subfolders based on the first 9 characters of their file name, BS01_1234.
I have the following so far but no luck.
this code worked to move a file:
move /-y "C:\Users\jtennent\Documents\testbatchfile\**.jpg" "\\autrsrv02\Tempdata\Justine Tennent\movetofolder"
pause
but i tried this to create sub folders and move with no luck
#ECHO OFF
SETLOCAL
SET "sourcedir=*C:\Users\jtennent\Documents\testbatchfile*"
PUSHD %sourcedir%
FOR /f "tokens=1,2,3,4 delims=_" %%a IN (
'dir /b /a-d "*_*.jpg"'
) DO (
MD %%a_%%b 2>nul
MOVE "%%a_%%b_%%c_%%d" .\%%a_%%b\ >nul
)
POPD
GOTO :EOF
Any help would be appreciated.
Edit
The following groups but splits too early
#echo off
for %%A in (.psd *.jpg) do (
for /f "tokens=1 delims=_" %%D in ("%%~nA") do (
md "%%D" 2>nul
echo Moving file %%A to folder %%D
move "%%A" "%%D" >nul
)
)
echo Finished
pause
Below is some untested example code, (it should move the file to the new location also removing the first ten characters from the file name in the new location)
#Echo off
Set "SrcDir=%UserProfile%\Documents\testbatchfile"
Set "DstDir=\\autrsrv02\Tempdata\Justine Tennent\movetofolder"
If Not Exist "%DstDir%\" Exit/B
If /I Not "%CD%"=="%SrcDir%" PushD "%SrcDir%" 2>Nul||Exit/B
For /F "EOL=_ Tokens=1,2* Delims=_" %%A In (
'Where .:"????_????_*.psd" .:"????_????_*.jpg"') Do (
If Not Exist "%DstDir%\%%A_%%B\" MD "%DstDir%\%%A_%%B"
Echo Moving "%%A_%%B_%%C" to "%DstDir%\%%A_%%B\%%C"
Rem Move /-Y "%%A_%%B_%%C" "%DstDir%\%%A_%%B\%%C")
Echo Finished
Pause
Change the locations in lines 2 & 3 to suit your requirements. If you're happy with the output change line 12 to:
Move /-Y "%%A_%%B_%%C" "%DstDir%\%%A_%%B\%%C">Nul)
and invoke the script again.
i want to create a dos script (.bat) to search on all sub folders and whenever it finds a file with the word MK11 in the file name it must create a folder named archive and move the file in it.
example:
c:\folder1\folder2\folderX\fileMK11.txt -> c:\folder1\folder2\folderX\archive\fileMK11.txt
c:\folder1\folder3\fMK11ile.txt -> c:\folder1\folder3\archive\fMK11ile.txt
I tried to make the following script from examples i have seen but the problem is that it creates the folder "archive" in the directory where the script is instead of the directory where the file is found.
setlocal ENABLEDELAYEDEXPANSION
set /a c=0
FOR /R %%i in (*MK11*) do (
set /a c=c+1
md archive
move "%%i" archive
)
endlocal
I think this script will get you down the road. I echoed a COPY command rather than a MOVE command, but some of the hard part is done.
#echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET TEMPFILE=%TEMP%\afinder_%RANDOM%_%RANDOM.tmp
DIR /S /B /A-D *MK11* >%TEMPFILE%
FOR /F "usebackq delims=" %%f IN (`type %TEMPFILE%`) DO (
ECHO "%%f"
FOR /F "delims=\ tokens=*" %%a IN ("%%f") DO (
SET PNAME="%%~pa"
ECHO PNAME is set to !PNAME!
ECHO "!PNAME:~-9,7!"
REM Check to see if this file is already in an Archive directory.
IF "!PNAME:~-9,7!" == "Archive" (
echo got one
) else (
echo not one
IF NOT EXIST "!PNAME!\Archive" (MKDIR "!PNAME!\Archive")
echo COPY %%f "!PNAME!\Archive"
)
)
)
IF EXIST "%TEMPFILE%" (DEL "%TEMPFILE%")
EXIT /B 0