I have some directories with names in format:
companyName_a13d0116cc
(unique name + underscore + random string)
Is it possible to rename all directories using just CMD like this?
companyName_a13d0116cc -> companyName
This is possible with the following way:
#echo off
for /F "delims=" %%A IN ('dir /B /AD "*_*"') do (
for /F "delims=_" %%B IN ("%%A") do ren "%%~fA" "%%B"
)
For cmd one-line use:
for /F "delims=" %A IN ('dir /B /AD "*_*"') do for /F "delims=_" %B IN ("%A") do ren "%~fA" "%B"
Related
I have a Windows CMD script that has a bug. The script is supposed to match the first 8 digits (the date) of a file with a directory titled with the same first 8 digits (the date). If successful, the file is moved into that directory & placed in a subfolder (called 'portfolio'). However, the error File not Found is returned.
file: 20230202_example.jpg
directory: 20230202_winter-holiday/portfolio
...the CMD file:
#echo off
for /f "delims=" %%a in ('dir /b /a-d') do (
set "filename=%%a"
set "first8=!filename:~0,8!"
for /f "delims=" %%b in ('dir /b /a-d *%first8%*') do (
if /i "!filename!" neq "%%b" (
move "!filename!" "%%b\portfolio\!filename!"
)
)
)
If I interrogate the directory in Command Prompt:
dir /b /a-d
...I get a full list of the files contained. When the script is run from Command Prompt, for each file contained I get:
File Not Found
Based upon your target file and directory names, you could probably do it without defining variables and therefore the need to delay variable expansion.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /R /C:"^202[0123]1[012][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]0[123456789]_."
/C:"^19[789][0123456789]0[123456789][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]3[01]_." /C:"^202[0123]0[123456789]3[01]_."
/C:"^19[789][0123456789]1[012]0[123456789]_."
/C:"^19[789][0123456789]1[012][12][0123456789]_."
/C:"^19[789][0123456789]1[012]3[01]_." /C:"^20[01][0123456789]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789]0[123456789]_." /C:"^202[0123]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789][12][0123456789]_."
/C:"^20[01][0123456789]0[123456789]3[01]_."
/C:"^20[01][0123456789]1[012]0[123456789]_."
/C:"^20[01][0123456789]1[012][12][0123456789]_."
/C:"^202[0123]0[123456789]0[123456789]_." /C:"^202[0123]1[012]0[123456789]_."
/C:"^202[0123]0[123456789][12][0123456789]_."
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1
I decided to use a little more accuracy in the dates, (it's not perfect because it has no knowlege of how many days are in each month of any year, but should cover dates between 19700101 and 20231231). If you don't want that, you could simplify it by just removing lines 4 through 17:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1
How to save a log .txt file of all deleted files with this code?
Also I need to change code to search for all backup folders in root directory subfolders.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
endlocal
This code was written by Mofi and posted as answer on How to remove oldest folder(s) of a group of folders in a folder with several folder groups? He posted also an enhanced version logging the files before deletion, but it was not working for me properly.
I tried with the followed command >> but log.txt file has not been generated.
The attempt to use >> was not shown in the question. How about something like:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set "BackupFolder=D:\backup"
set "LastDate="
SET "LOGFILE=%TEMP%\%~n0.log"
for /F "delims=." %%I in ('dir "%BackupFolder%\????????.*" /AD /B /ON 2^>nul') do (
if not "!LastDate!" == "%%I" (
for /F "skip=2 delims=" %%D in ('dir "%BackupFolder%\%%I.*" /AD /B /O-D-N /TC') do (
DIR /S /B "%BackupFolder%\%%D" >>"%LOGFILE%"
rd /Q /S "%BackupFolder%\%%D"
set "LastDate=%%I"
)
)
)
endlocal
I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough
for /f "tokens=* delims=" %%a in ('dir "recordings.txt" /s /b') do (
echo %%a
)
Is this the correct format to look on the Z: drive for files in the recordings.txt?
Tried powershell don't have permissions on the server
Contents of the file just have the file name / extension
3030009948_3030009912_df1389947f0fb80d62832122.sasf
The Directory structure of Z: is as follows
MM\dd\hh\mm\
recordings.txt is on the Desktop of my userprofile
I also need the paths of the found files
This should read recordings.txt on your desktop and create recordings-results.txt in the same place with the full path to every file inside it.
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
del "%userprofile%\desktop\recordings-results.txt" 2>nul
for /f "usebackq delims=" %%a in ("%userprofile%\desktop\recordings.txt") do (
echo finding "%%a"
findstr /i /c:"%%a" "%temp%\results.tmp" >>"%userprofile%\desktop\recordings-results.txt"
)
del "%temp%\results.tmp"
pause
This should be even faster:
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
findstr /i /g:"%userprofile%\desktop\recordings.txt" "%temp%\results.tmp" >"%userprofile%\desktop\recordings-results.txt"
del "%temp%\results.tmp"
pause
Dir is to look into a directory if you want to get a value from a file use :
for /f "tokens=*" %%a in ('type "Z:\recordings.txt"') do (
echo %%a
)
I have a folder with all jpg and this is format 1UYK08HJ_20140403165858071_SYPTE1-PC but i want it rename like this
1UYK08HJ_SYPTE1-PC_20140403165858071.jpg. but when i run this script it always ends up with
.%~nA_1UYK08HJ.jpg.jpg.jpg
this is my batch script..
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "eol=: delims=" %%F in ('dir /b /a-d *_*.jpg') do (
for /f "tokens=1* eol=_ delims=_" %%A in ("%%F") do ren "%%F" "%%~nA_%%B%%~xF"
)
popd
This should do it:
#echo off
pushd "C:\Users\IT-Administrator\Desktop\export" || exit /b
for /f "tokens=1-4 delims=_." %%A in ('dir /b /a-d *_*.jpg') do (
Echo ren %%A_%%B_%%C.%%D %%A_%%C_%%B.%%D
)
popd
Remove the echo when you see correct output.
In the (Window's) folder's command line(cmd) run
ren *.* *.jpg