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)
)
Related
I have about 150 folders with .7z files that I want to extract all the .7z files in order into a different directory, "Z:\master statistics" and automatically overwrite. Below is the batch I'm working with, but I am unsure how to extract the .7zfiles and skip the first parent folder in the .7z.
For example:
sales2015.7z file has the below directories
sales/engines/parts
sales2010.7z file has the below directories
sales/equipment/parts
When it extracts, I am trying to get it to extract from the second directory. So if the batch worked correctly, the output directory would only have the Engine & Equipment folders and all its subdirectories. If a file with the same name exists, it automatically overwrites.
FOR /D /r %%F in ("*") DO (
pushd %CD%
cd %%F
FOR %%X in (*.7z) DO (
"C:\Program Files\7-zip\7z.exe" x -o "%Z:\master statistics%"
)
popd
)
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.
I have a parent folder with lots of folders (movies) underneath. Each folder has 1 file (the actual movie).
I would like some advice on a batch script that I can run to rename the folders to the file (movie) within excluding the extension (.avi)
e.g.
BEFORE
Parent (folder)
Folder 1
Movie 1.avi
Folder 2
Movie 2.avi
AFTER
Parent (folder)
Movie 1
Movie 1.avi
Movie 2
Movie 2.avi
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\*" '
) DO (
FOR %%b IN ("%sourcedir%\%%a\*") DO IF /i "%%~na" NEQ "%%~nb" ECHO(REN "%sourcedir%\%%a" "%%~nb"
)
GOTO :EOF
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
You would need to set your directory as sourcedir. I set my testing directory.
The following batch file does the job:
#echo off
if exist "%TEMP%\RenameFolders.bat" del "%TEMP%\RenameFolders.bat" >nul
cd /D "Complete\path\to\parent\folder"
for /R "Complete\path\to\parent\folder" %%I in (*.avi) do echo ren "%%~dpI" "%%~nI">>"%TEMP%\RenameFolders.bat"
if exist "%TEMP%\RenameFolders.bat" (
call "%TEMP%\RenameFolders.bat"
del "%TEMP%\RenameFolders.bat" >nul
)
"Complete\path\to\parent\folder" must be modified twice in the batch file by real parent folder path.
The main job is done by command FOR. Run in a command prompt window for /? to get information about option
/R ... recursive folder/file scan, and
%%~dpI ... drive and path of file, and
%%~nI ... name of file without path and file extension.
For each *.avi file the command echo is executed to create the rename command used later to rename the folder containing the *.avi file to the name of the *.avi file.
It is not possible to do the folder rename directly within the FOR loop as it is not possible to rename a folder while it is the current working directory for a running process. Each folder is the current working directory for the batch process while FOR is running.
The command CD in third line just makes sure that the working directory is set to parent folder of all the subfolders to rename to avoid that renaming a folder fails because the batch file is started from one of the subfolders.
The second line just makes sure there is no file RenameFolders.bat in the folder for termporary files for example when terminating this batch file once by click on button X while command FOR is currently running.
After command FOR processed recursively all *.avi files and a line with command ren was appended to file RenameFolders.bat for each file, this created batch file is called which renames now the folders.
Last the batch file temporarily created is deleted as not needed anymore.
I have a folder with a bunch of subfolders. How would I wirte a bat file to append 8 random characters to the end of the folder names.
The first step I went through was placing files into the subfolders below, with this code:
for %%i in (PathToWorkingFolder\*) do mkdir "PathToWorkingFolder\%%~ni" & move "%%i" "PathToWorkingFolder\%%~ni"
The folder names are :
FD3_2012-10_Stmt
FD3_2012-10_Tax
FD3_2012-10_Warr
The two steps may be achieved in the same FOR:
#echo off
setlocal EnableDelayedExpansion
for %%i in (PathToWorkingFolder\*) do (
rem Get folder name with 8 random digits at end
digits=000000!random!!random!
set "folder=%%~Ni!digits:~-8!"
rem Create the subfolder and move the file
mkdir "PathToWorkingFolder\!folder!"
move "%%i" "PathToWorkingFolder\!folder!"
)
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.