Windows Batch: Rename a folder to the name of a file inside - windows

I have ~3000 folders, each with multiple files in it. Each folder contains a txt file, with a few other file types. The folder names are hashed, so right now it's a jumble of random digits. I want to rename each folder to the same name as the txt file that is inside that folder. For example:
123456/myfile.txt
Should become:
myfile/myfile.txt
The folders do not contain any subfolders, if that matters.
Any help is greatly appreciated!

for /d %%a in (*) do (
for %%b in ("%%a\*.txt") do (
ECHO ren "%%a" "%%~nb"
)
)
use a for /d to iterate over your folders and another plain for to get the filename (assuming, there is exactly one .txt file in each folder). %%~nb gets the name only without extension).
NOTE: after troubleshooting, remove the ECHO to enable the rename command.

Related

How to batch copy files based on a list (txt) in to another folder with same directory structure?

I have a root directory with over 25,000 files in it. These files are in loads of different subdirectories.
I also have a text file with 4300 lines in it, each line is an absolute path to one of the files in the directory. Like below,
c:\dir1\hat1.gif
c:\dir1\hat2.gif
c:\dir1\dir2\hat1.gif
c:\dir1\dir2\hat2.gif
c:\dir1\dir3\cat.zip
c:\dir1\dir3\banana.exe
I also have another root directory witch is a copy of the original root directory structure but all the directories are empty.
I would like to copy all the files listed in the text file to the directory which is empty and place all the copied files inn the respected subdirectories.
if I use the following batchfile I keep getting file overwrite prompts because it is not copying the files to the correct directories.
#echo off
set dst_folder=c:\DSTN2
for /f "tokens=*" %%i in (USEDFILES.txt) DO (
xcopy /S/E "%%i" "%dst_folder%"
)
How do I modify this so the files are copied to the correct directory?
Since you are copying specific files from a list, you need to make sure the directory structure exists in the destination if you want it in a similar folder structure. So using the power of the FOR command modifiers you can get the file path only from the file name in the file list. You will use that modifier to create the destination directory and also use it as the destination for the XCOPY command.
I have taken the liberty of providing best practices for all the code you are using.
#echo off
set "dst_folder=c:\DSTN2"
for /f "usebackq delims=" %%G in ("USEDFILES.txt") DO (
mkdir "%dst_folder%%%~pG" 2>NUL
xcopy "%%~G" "%dst_folder%%%~pG"
)

Windows Batch script to rename files with it's folder name within a for loop

I have a bunch of .flv files in subdirs and I need to loop through them and rename it according to its path.
I was able to loop through them all, but I don't know how to split the path and rename it using batch script.
Here's what I have so far:
echo off
for /R %%F in (*.flv) do (
echo %%~pF
)
The "echo %%~pF" prints the path for the current file on the loop, something like this:
\folder\morefolders\activity\ NameThatIwant \Videos\
I tried spliting with "delims=\" on my for loop but I get only "echo off".
I tried other tutorials, read other questions on SO but none of them were renaming the files from a split string from the path of the file in the loop.
Could you guys help giving suggestions or direct me to any material that explains those %% codes?
Thanks.
I think you do not need to split the path, though you could do it using for /f "delims=NameInthePath tokens=1", where NameInthePath - is some word in the path and tokens= gives you the first part of the path separated by delims.
Actially, if you need to rename file name you need to use REN command. If you need to change the path for the flv file - use copy of move command.
A batch file to rename all .LOG files to .TXT in the 'demo' folder and all sub-folders:
CD C:\demo\
For /R %%G in (*.LOG) do REN "%%G" "%~nG.TXT"

Merge all files from the current folder and all subfolders in one file using .bat file

I'm looking for a batch file that merges the content of all files in the current directory and files from all subdirectories.
Also, that would be perfect if files were separated by several new lines in the big file and probably contain the filename with filepath above the each file content.
For example, there are two files in the current folder D:\ , where our batch file is located:
1.
d:\file1.txt contains:
some
text here
hahaha
2.
d:\folderabc\file2.mp3 contains:
doremi text
I run merge.bat file on d:\ and it creates a merge file result.txt (or whatever extension) with such content:
=========d:\file1.txt=========
some
text here
hahaha
=========d:\folderabc\file2.mp3=========
doremi
I appreciate if someone share his solution for this problem.
Thank you so much.
Test this with your folder. It assumes the files are text files.
edit: This has a fix - and you can remove the "d:\base\folder" to run it from the current directory.
#echo off
for /r "d:\base\folder" %%a in (*) do (
(
echo =========%%a=========
type "%%a"
echo(
echo(
echo(
)>>"%temp%\temp.file"
)
move "%temp%\temp.file" . >nul
echo done
pause

Read the filenames of group of files , make folders with specific file name(excluding extentions) than copy each file in their respective folder.

Read the filenames of group of files , make folders with specific file name(excluding extentions) than copy each file in their respective folder.
I had tried with this ,not working right
#echo off
for /F "tokens=1 delims =_" %%G in ('dir/b *.txt') do set testvariable=%%G
md %testvariable%
set "path=C:\Documents and Settings\USP\Desktop\"
set "path2=%testvariable%"
set "destpath=%path%%path2%"
copy *.txt %destpath%
You cannot create a folder with same name as a file on the same location.

copy list of files whose paths are in semi colon delimited source.txt file to destination folder keeping their souce paths

i have list of files in test.txt which contains list of file paths in format d:\source\www\default.aspx;d:\source\common\common.js I need to write a bat file to copy these files to destination eg.F:\destination\ whose path is also passed as an parameter to the bat file.I have following script for this for /f %%l in (somefile.txt) do (
for %%f in (%%l) do (
copy "%%f" %1
)
) issue is i need to keep the copy source folder's folder structure in destination folder too. ie above d:\source\www\default.aspx need to copy to f:\destination\www\default.aspx not to f:\destination. Will gratefull if some one can give solution to this.
Please try with xcopy /I "%%f" "%~1\%%~pf":
xcopy will create the directory structure for you (without prompting because of the /I switch);
%%~pf is the path-only part of the file to copy (see help for), appended to your destination base path without any surrounding quotes %~1;
the destination path combination is enclosed in quotes.

Resources