Rename a file the name of the directory that it is in - batch-file

I think it's a simple task, but I'm a noob to batch scripting.
Here is what the structure looks like:
MAINDIR
directory name
nameof.file
I'm looking to batch rename all the files in the directories the same name as the directory.
Basically it is:
Scan the subfolders and find all *.file.
Rename the *.file to the name of the directory it is already in
The end result is nameof.file to be directory name.file
To be more specific, I have 350 files that i need to rename. They are all in their appropriate directory and I want the 1 file inside each folder to have the name of the folder as the file name.
c:\folder 1\file1.ext
c:\folder 2\file2.ext
c:\folder 3\file3.ext
All the .ext files need to have their folder names as file name.
c:\folder 1\folder 1.ext
c:\folder 2\folder 2.ext
c:\folder 3\folder 3.ext
I hope that makes it more clear. So once the script is prepared, I can drag and drop all 350 files and one by one onto the script and it will take the file and rename it.

This will rename any file that is dragged onto it to it's parent directory name.
setlocal enabledelayedexpansion
for %%a in (%1) do (
set p=%%~dpa
set p=!p:~0,-1!
for %%b in ("!p!") do ren %%a "%%~nb.*"
)
To do this recursively through all files in all subfolders under a fixed root directory you can use this
setlocal enabledelayedexpansion
for /r C:\folder %%a in (*) do (
set p=%%~dpa
set p=!p:~0,-1!
for %%b in ("!p!") do ren "%%a" "%%~nb.*"
)
Which will save you having to drag and drop them all individually. Unless you can only do it one at a time, in which case the first solution will suffice.

Related

Renaming files to folder names

I currently have a script I have been working on for a few hours and having some trouble. What it does is takes all .dat files in the C:\TEST\WF\ directory and renames it to the folder name plus .txt. However it is not renaming the files to the folder name, it grabs the previous folder instead
Example:
Source Directory:
C:\TEST\WF\FOLDER1\FILE.dat
C:\TEST\WF\FOLDER2\FILE.dat
C:\TEST\WF\FOLDER3\FILE.dat
Current output:
C:\TEST\WF\FOLDER1\WF.txt
C:\TEST\WF\FOLDER2\WF.txt
C:\TEST\WF\FOLDER3\WF.txt
Expected output
C:\TEST\WF\FOLDER1\FOLDER1.txt
C:\TEST\WF\FOLDER2\FOLDER2.txt
C:\TEST\WF\FOLDER3\FOLDER3.txt
#echo off
setlocal EnableDelayedExpansion
for /d %%F in (C:\TEST\WF\*) do (
for %%* in (%%F) do set CurrDirName=%%*
IF EXIST "%%F\*.dat" (REN "%%F\*.dat" %CurrDirName%.txt)
)

How to move files to a new folder or subfolders based on parts of the file name?

I have a folder where lots of .tif files drop into.
They all start with MW_ or SW_ and later also NSSW. So I need to be able to expand from the first two later to NSSW.
I have a batch file already that first moves the files into the folder MW or SW based on the first 2 characters of the file name. Here is my current batch file and it works just fine. But I think I need a second batch file or addition to this to do the below 1 & 2 steps. Please see below, after this code.
REM Sort by First name.
REM This script creates a folder for either the full file name,
REM or if it contains an underscore, the part before the underscore.
REM TODO - Don't copy over existing files.
REM TODO - Move files into Sub folders based on Date in file name "last 8 characters .tif
#echo off
REM Needed because you are working with variables that are immediately called
setlocal enabledelayedexpansion
REM Start of the loop to get all files with a psd or jpg Extension
for %%A in (*.tif *.jpg *.pdf) do (
echo file found %%A
REM Grabs only the file name
for /f "delims=" %%B in ("%%A") do set fname=%%~nB
REM Grabs only the extension
for /f "delims=" %%C in ("%%A") do set fextn=%%~xC
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
REM Moves the file to the folder
echo Moving file %%A to folder !folname!
REM if not exist "%%D\%%A" move "%%A" "!folname!"
if not exist "%%D\%%A" copy "%%A" "!folname!"
REM add the date DDMMYYYY to the end of each file. Name can be 80 characters long.
rem ren "!folname!\%%A" "????????????????????????????????????????????????????????????????????????????????_%date:~-10,2%%date:~-7,2%%date:~-4,4%.tif"
)
echo Finished
pause
So here is what I need I guess. A second or 3rd batch file to do the below. I hope, someone can help.
Note: Please keep in mind that if a file exists it renames the copy when moving with xxxx(1).tif, xxx(2).tif, etc. on the end of file name.
Move files now listed in MW folder into new or existing subfolders based on the name of the file from the 4th to 10th character of the file name or from the 4th character to the second or next "_".
Move file in subfolder of the named folder to a sub date named folder based on last 8 characters of file name.
What I need is to then move the files from the MW folder into new or existing subfolders based on the last 7 characters of the file name "the date section".
For example we start with MW files
Files coming in to folder C:\temp\Test_Moving_Files\
MW_VRL5VF10000_6542234_01052016.TIF
MW_Flybuys_677888_01052016.TIF
MW_VRL5VF10000_333443_02052016.TIF
MW_Flybuys_555555_02052016.TIF
MW_goodguys_534535_02052016.TIF
MW_goodguys_222222_02052016.TIF
MW_Flybuys_123443_03052016.TIF
MW_Flybuys_3545555_03052016.TIF
MW_goodguys_444444_03052016.TIF
MW_goodguys_888888_03052016.TIF
Output to subfolders should be sorted to subfolders like below:
MW\VRL5VF10000\01052016\MW_VRL5VF10000_6542234_01052016.TIF
MW\VRL5VF10000\02052016\MW_VRL5VF10000_333443_02052016.TIF
MW\Flybuys\01052016\MW_Flybuys_677888_01052016.TIF
MW\Flybuys\02052016\MW_Flybuys_555555_02052016.TIF
MW\Flybuys\03052016\MW_Flybuys_123443_03052016.TIF
MW\Flybuys\03052016\MW_Flybuys_3545555_03052016.TIF
MW\goodguys\01052016\MW_goodguys_222222_02052016.TIF
MW\goodguys\02052016\MW_goodguys_534535_02052016.TIF
MW\goodguys\03052016\MW_goodguys_444444_03052016.TIF
MW\goodguys\03052016\MW_goodguys_888888_03052016.TIF
It looks the task can be achieved by modifying
REM Using the File name it separates it into 2 part using "_" as a delimiter so 120_low becomes 120 and low
for /f "tokens=1* delims=_" %%D in ("!fname!") do set folname=%%D
echo folder name !folname!
REM Checks for the existence of the folder, if the folder does not exist it creates the folder
if not exist "!folname!" (
echo Folder !folname! does not exist, creating
md "!folname!"
) else (
echo Folder !folname! exists
)
to
REM Using the file name it separates it into 4 parts using "_" as a delimiter.
for /f "tokens=1-4 delims=_" %%D in ("!fname!") do set "folname=%%D\%%E\%%G"
echo Folder name !folname!
REM Checks for the existence of the folder path. If the folder
REM path does not exist, it creates the complete folder structure.
if not exist "!folname!\*" (
echo Folder !folname! does not exist, creating ...
md "!folname!"
) else (
echo Folder !folname! exists.
)
Instead of using just first substring of underscore delimited file name, the code above uses now first, second and fourth substring. I did not test it!
Command MD can create also multiple folders at once and therefore variable folname can be also a folder path string. Well, I suggest to rename the variable folname in your batch code for example to FolderPath.
Of course you must use folname or better FolderPath also in rest of batch script instead of %%D.

Batch Unzip, Rename zip contents to original zip file name, Rezip

I found this script here that works well (thanks MC ND!). However, I dont want to retain the original name that was in the zip file. Please see below:
#echo off
setlocal enableextensions disabledelayedexpansion
for %%z in (*.zip) do (
if not exist "%%~nz" md "%%~nz"
7za e -o"%%~nz" "%%~fz"
for %%f in ("%%~nz\*") do ren "%%~ff" "%%~nz - %%~nxf"
)
For each zip file, if a folder with the same name does not exist, create it. Extract the zip file into the new folder. For each file inside the folder, rename the file to the name of the zip followed by the original name of the file.
I would like to remove the bold part from the script. Thank you!

Using WINRAR with batch script

I am new to scripting, can some please assist me,
I have batch file that
Looks at the first 8 characters in the file name, creates and
moves those files to new folder with first 8 characters as folder
name.
Then looks at folder created in step 1 for next four series
of character (9,10,11,12)and create and move to another subfolder
with next 4 characters as folder name.
Then looks at folder created in step 2, for extension of every file and create and move
to a new folder with extension as folder name.
For example, I have files that look like this
ABCEFGHI0703xyz.pdf
STUVWXYZ0805xyz.pptx
Move to folder
ABCEFGHI\0703\PDF
STUVWXYZ\0805\PPTX
Keeping in mind first 8 characters are random, next 4 character are year and month, and 9 types of extensions.
I am using this batch script to create these folders:-
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\sourcedir"
SET "destdir=C:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*" '
) DO (
SET name=%%~na
SET ext=%%~xa
SET name=!name:~0,8!\!name:~8,4!\!ext:~1!
MD "!name!" 2>nul
MOVE "%sourcedir%\%%a" "!name!\" >nul
)
GOTO :EOF
Now I would like to add a WINRAR command to archive just the extension folders created in step 3, I am using this command to create the archives.
C:\ ABCEFGHI\0703\PDF>WINRAR A PDF C:\ ABCEFGHI\0703\PDF
Is it possible to add this command to the script?
Ok first you need to have rar.exe in a folder in %PATH%,
i'd suggest you put a link in your Windows\System32 folder like so:
mklink C:\Windows\System32\rar.exe "C:\Program Files\WinRAR\rar.exe"
then you can get to work.
As you already suggested, first create the desired directory tree and then just add the required files to your archive like so:
rar.exe a %ARCHIVE_NAME% MainFolder\*.pdf
rar.exe a %ARCHIVE_NAME% MainFolder\FolderA\*
rar.exe a %ARCHIVE_NAME% MainFolder\FolderB\*
Whereas %ARCHIVE_NAME% is the file name of your new target archive (such as foo.rar)
This will every *.pdf file in 'MainFolder' and everything in 'FolderA' and 'FolderB'. The directory tree will be preserved.
Also, you may want to check whether %ARCHIVE_NAME% already exists, since rar will just add the specified files to an existing archive (possibly overriding them)
Hope this clarifies some things for you.
Edit: doing this recursivly for unknown root directory
set ARCHIVE_NAME=%CD%\pdf_archive.rar
for /r %CD% %%d in ('PDF') do (
if exist "%%d" (
echo Archiving files in: %%d
rar a "%ARCHIVE_NAME% "%%d"\*
)
)
Now this will go into every subdirectory recursivly (starting from your current directory)
Then iw will look for folders called 'PDF' and if they exist it will archive every file in that folder to %ARCHIVE_NAME%

CMD Batch Create folders by files names and put it inside

I have about 1000 files like:
_etc_sec
_home_host_www_temp
and etc.
How to make batch files to make folder inside each other depending on it filenames and then put this file into it.
All files are on d:\
So if we have file named _etc_sec we must create folder named d:\etc then put file _etc_sec into file d:\etc
So if we have file named _home_host_www_temp we must create folder named d:\home then inside this folder create folder host and then inside folder host create folder www then put file _home_host_www_temp into file d:\home\host\www
I know how do that with VBA but batch cmd is to difficult for me. Thanks.
Use SET search and replace to change _ into \, then use FOR the variable ~p modifier to get the full path that needs to be created. A single MKDIR command can make a series of nested directories. I toggle delayed expansion on and off within the loop just in case you have a filename with an ! character.
#echo off
setlocal disableDelayedExpansion
for %%F in (*_*) do (
set "file=%%F"
setlocal enableDelayedExpansion
for %%A in ("!file:_=\!") do (
endlocal
2>nul mkdir "%%~pA"
>nul move "%%F" "%%~pA"
)
)

Resources