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

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.

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: Rename a folder to the name of a file inside

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.

Create subfolder from suffix of filename then move file

I have got several image files in one folder sharing the same filename but have a different suffix
i.e:
textures\texture_suffix.dds
textures\texture_suffix1.dds
...
textures\texture_suffix10.dds
Using a batch I want to create subfolders named Texture.Suffix, Texture.Suffix2 based on the suffix of the files under textures, then move the respective file into its subfolder and rename it 'texture.dds'.
Can this be done using batch file?
Thanks for any help.
cd textures
for /f %%i in ('dir /b /s') do (
md %%~ni
move %%i %%~ni\ )
pause
write these tags in a bat file and run the bat file in the same directory that includes textures directory

Copy files listed in a txt file and put it in another directory

Im trying to copy multiples/hundreds of files using a text file and placing it in another directory
For example my source directory has this files.
09_yehey_123456.SENT
09_yohoo_987654.SENT
09_testy_789065.SENT
09_lolol_124125.SENT
09_hahah_241567.SENT
And the text file looks like this (this text file contains the files that I should only copy from the source directory)
09_yehey
09_yohoo
09_testy
Here's the code that i tried using
for /f "delims=" %%i in (files2.txt) do echo F|xcopy "C:\Users\username\source\%%i*" "C:\Users\username\Documents\dest\%%i"
PAUSE
the * from the source dir doesnt seem to work as a wildcard
Try like this :
for /f "delims=" %%i in (files2.txt) do (
copy /y "C:\Users\username\source\%%~ni*" "C:\Users\username\Documents\dest\"
)
PAUSE
As the names in the text file are not the same as the actual files you'll need wild cards.

copy file based on content - command prompt

I got a folder with bunch of files in it. each file name has unique date ex: 20140101. each file has data of one product.. ex: 20140101 has data for product "oranges" file 20140102 has data of product "apple" .. I need to create a script which will check the content of file and if match found, copy the file to different directory.
Example:
Find "Oranges" in
C:\Data\
found 2 files .. 20131229 and 20140101
copy files to c:\oranges\
Thanks in advance.
#echo off
setlocal enableextensions disabledelayedexpansion
set "source=c:\data\*.*"
set "target=c:\"
set "classes=orange apple"
for %%c in (%classes%) do (
if not exist "%target%%%c\" md "%target%%%c\"
for /f "delims=" %%f in ('findstr /m /l /c:"%%c" "%source%"') do (
copy "%%~ff" "%target%%%c\"
)
)
For each of the indicated classes, create target directory if it does not exist, and for each file with the indicated content, move it to the adecuated directory (not tested, adapt as needed)

Resources