Finally I got to move files with different extensions into folders according to their extensions, but I cannot get rid of the "." preceding the created folders... Here's my batch file:
for %%i in (*) do mkdir "%%~xi" & move "%%i" "%%~xi"
Please help me remove the dot. Thanks
One way:
setlocal enabledelayedexpansion
for %%i in (*) do (
set ext=%%~xi
set ext=!ext:~1!
mkdir "!ext!"
move "%%i" "!ext!"
)
What you are doing is just creating a folder names after the file extension. To remove the dot all you need to do is remove the first character.
setlocal enabledelayedexpansion
for %%i in (*) do (
set "x=%%~xi"
set x=!x:~1,400!
mkdir "!x!"
move "%%i" "!x!"
)
pause `
If you by chance just want to rename the folders:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%D in ('dir /a:d /b') do (
set x=%%D
set x=!x:~1,100!
set y=!x:~0,1!
if "!y!"=="." (
rename "%%D" "!x!"
)
)
pause
Related
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)
I have a requirement to remove the space from the folder name in a particular directory. e.g. Project-A is my directory under which have Phase1 Testing, Phase1 Prod, Phase1 UAT subdirectories. I want a batch script which will rename the subdirectories to Phase1Testing, Phase1Prod, Phase1UA names.
you could try something like this:
#echo off
FOR /f "delims=" %%G IN ('dir /ad /b') DO (
setlocal enabledelayedexpansion
pushd "%%~dpG"
SET fname=%%~nxG
SET fname=!fname: =!
rename "%%~nxG" "!fname!"
popd
endlocal
)
You can find more information in: replace_spaces_with_dashes and in spaces_in_file_names
I hope this help you!
Thanks #Dayana for the help. Below batch script is working fine and removing the space from the current directory.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /s /b /ad') DO (
SET Var=%%~na
SET Var=!Var: =!
REN "%%a" "!Var!"
)
I picked up the code below from another post. I believe it should pick up the current directory folder and include it in the renaming part of the process, however that doesn't seem to work for me.
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=0
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1!inc!%%~xa"
Ren "%%a" "%~n1!inc!%%~xa"
)
POPD
I have a .txt file that will be received into a folder each day named and time stamped. Example as below:
FileNameA_20170418153000.txt
Essentially I'd like to amend the code above to rename the file: filenam0001.txt and continue to update the sequence number (which works perfectly well).
i.e.
filenam0001.txt
filenam0002.txt
filenam0003.txt
Any help would be greatly appreciated.
you need to add the leading zeros manually (add some zeros, then cut the last x characters):
#echo off
setlocal EnableDelayedExpansion
set inc=0
for /l %%a in (1,1,50) do (
set /a inc+=1
set num=00000000!inc!
set num=!num:~-5!
echo !num!
)
I modified your code in order to insert the leading zeros in a simple way...
#ECHO OFF
setlocal enabledelayedexpansion
PUSHD "%~1"
set inc=10000
FOR /f "delims=" %%a in ('dir /b /a-d') DO (
set /a inc+=1
Echo Ren: "%%a" "%~n1_%%~na!inc:~1!%%~xa"
Ren "%%a" "%~n1_%%~na!inc:~1!%%~xa"
)
POPD
I have files with varying names...
Tim-01.jpg
Tim-02.jpg
Tim-03.jpg
jack-01.jpg
jack-02.jpg
jack-03.jpg etc in a single folder
I want to move all tim files into Tim folder and jack files to Jack folder etc.
Can it be done using .bat files ? If yes, please share the code for it.
Thank You.
#echo off
setlocal
set sourcedir=c:\sourcedir
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d "%sourcedir%\*-*.*") do (
md "%sourcedir%\%%a" 2>nul
echo move "%sourcedir\%%a-%%b" "%sourcedir%\%%a\"
)
Note that the 2>nul suppresses error messages created when an attempt is made to recreate an existing directory
The MOVE is merely ECHOed. Remove the ECHO keyword to activae the move. It may also be prudent to append >nul to the MOVE statement to suppress the "1 file(s) moved" message.
As you may know, there are no arrays in Batch. So let's just use one.
#ECHO OFF &SETLOCAL
for /f "tokens=1*delims=-" %%a in ('dir /b /a-d *-*.jpg') do set "$%%a=%%a"
for /f "tokens=1*delims==" %%a in ('set $') do robocopy "%cd%" "%cd%\%%b" "%%b*" /mov /l
Remove /l from robocopy to make it working.
EDIT - Changed code to reduce the number of file moves. Now, all files with the same prefix get moved in one command.
#echo off
setlocal enableextensions
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-*.jpg") do (
rem if the file still exists (maybe it has been moved)
rem then split the file name with the dash as delimiter
if exist "%%~ff" for /F "tokens=1 delims=-" %%s in ("%%~nf") do (
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "%%~s"=="" (
robocopy "%_dir%" "%_dir%\%%~s" "%%~s-*.jpg" /mov /njs /njh
)
)
)
endlocal
EDIT2 - Changed to adapt to comments
#echo off
setlocal enableextensions enabledelayedexpansion
rem source of images
set "_dir=."
rem for each jpg with a dash in its name
for %%f in ("%_dir%\*-??.jpg") do (
rem if the file still exists (maybe it has been moved)
if exist "%%~ff" (
rem get the filename without the 3 last characters
set "_target=%%~nf"
set "_target=!_target:~0,-3!"
rem and if we get a folder target, move the all the files
rem with same prefix to proper folder
if not "!_target!"=="" (
robocopy "%_dir%" "%_dir%\!_target!" "!_target!-??.jpg" /mov /njs /njh
)
)
)
endlocal
I have some software folders like these:
CCleaner 3.17
Firefox v.18.0.2
Avant Browser v2013.01.09
as you can see some of them have the "v" and "v." as version indicator and some does not have it.
I am trying to find a way to add "v." before the number in a folder and all of its subfolders using batch file. if it does not have "v" or "v." add "v." and if it has "v" replace it with "v." and in case if it has the "v." do nothing.
I will be grateful if you could please help me in this matter.
Thanks in Advance
This is a bit tricky. My solution assumes that the version number is the remaining text after the last space in the existing folder name.
The code below will appropriately rename all folders in the current directory that have at least one space in the name. It does not recurse into sub-folders. The code will break if the version text contains ! or =, though that is unlikely to be a problem.
#echo off
setlocal disableDelayedExpansion
for /d %%F in ("* *") do (
set "folder=%%F"
setlocal enableDelayedExpansion
for %%A in ("!folder: =\!") do (
for /f "eol=: tokens=* delims=v." %%B in ("%%~nxA") do (
if "%%~nxA" neq "v.%%~B" ren "!folder!" "!folder:%%~nxA=v.%%~B!"
)
)
endlocal
)
If you want to recurse into the sub-folders, then an outer loop is required, and the folders must be sorted in reverse order so that the deepest folders are processed first.
#echo off
setlocal disableDelayedExpansion
for /f "eol=: delims=" %%D in ('dir /s /ad /b * ^| sort /r') do (
pushd "%%D"
for /d %%F in ("* *") do (
set "folder=%%F"
setlocal enableDelayedExpansion
for %%A in ("!folder: =\!") do (
for /f "eol=: tokens=* delims=v." %%B in ("%%~nxA") do (
if "%%~nxA" neq "v.%%~B" ren "!folder!" "!folder: %%~nxA= v.%%~B!"
)
)
endlocal
)
popd
)