Copy files based on the extension length of the file? - cmd

i'm looking for a way on Windows to find all files with a specific file extension length and copy them to another location while preserving the folder structure.
For example lets say that I want to copy all files on my D: drive, with a file extension length of excatly six (*.******) and copy them to another location while also keeping the folder structure.
Is this possible in CMD?.

C:\Users\PeterR>copy C:\Users\PeterR\Documents\cmd\???.txt C:\Users\PeterR\Documents\cmd1\
--use ? to specify the lentgh. for six characters it's ??????.extension

What about the following code snippet:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_SOURCE=D:\"
set "_DESTIN=E:\"
pushd "%_SOURCE%" || exit /B 1
for /F "delims=" %%F in ('
xcopy /L /E /I ".\*.*" "E:\" ^| find ".\"
') do (
for /R "D:\" %%F in ("*.*") do (
set "EXT=%%~xF"
setlocal EnableDelayedExpansion
if "!EXT:~6!"=="" if not "!EXT!"=="!EXT:~5!" (
endlocal
move /Y "%%~F" "%_DESTIN%\%%~F"
) else endlocal
)
popd
endlocal
exit /B

Related

How to rename a file to the parent's parent folder name in batch

so I have a bunch of folders with similar directories to the below:
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png
I want to rename all the .png files the number just before the first randomStringofCharacters. So basically
.\page\1\randomStringofCharacters\randomStringofCharactersAgain.png -> 1.png
.\page\2\randomStringofCharacters\randomStringofCharactersAgain.png -> 2.png
.\page\3\randomStringofCharacters\randomStringofCharactersAgain.png -> 3.png
Is there any batch script that can do this? I have tried:
#Echo OFF
FOR /D /R %%# in (*) DO (
PUSHD "%%#"
FOR %%# in ("*.png") DO (
Echo Ren: ".\%%~n#\%%#" "%%~n#%%~x#"
Ren "%%#" "%%~n#%%~x#"
)
POPD
)
Pause&Exit
Yet this only renames the file with the parent directory.
And if possible is there a way to move all such renamed .png files in to a newly made folder in .\page\ (the same place where the .bat is) with the folder name of page?
Thanks in advance!
Could be something like this (you have to drag and drop the main folder to the batch):
#echo off
if exist "%~1" (IF not exist "%~1\" exit) else (exit)
if /i not exist "%~dp0Page" md "%~dp0Page"
pushd "%~1"
for /f "delims=" %%a in ('dir /s /b *.png') do Call :Rename "%%~a" "%%~dpa"
exit
:Rename
set Cpath=%~2
set Cpath=%Cpath:~0,-1%
For %%a in ("%Cpath%") do set Cpath=%%~dpa
set Cpath=%Cpath:~0,-1%
for %%a in ("%Cpath%") do set NName=%%~nxa
move "%~1" "%~dp0Page\%NName%%~x1"
goto :EOF
You should not use for /R to handle items in a predefined directory hierarchy depth:
#echo off
rem // Iterate through the numeric target sub-directories (`1`, `2`, `3`, etc.):
for /D %%K in (".\page\*") do (
rem // Iterate through sub-sub-directories `randomStringofCharacters`:
for /D %%J in ("%%~K\*") do (
rem // Iterate through `.png` files:
for %%I in ("%%~J\*.png") do (
rem // Actually rename the current file:
ren "%%~I" "%%~nxK%%~xI"
)
)
)
Note, that this approach does not check whether there is only one .png file at each location.

Search for files based on a list of partial names and copy them to a destination folder using windows shell

I am an absolute newbie to batch programming. I have posted this question after some amount of searching. Kindly guide me.
I have a folder containing a thousand images:
000001_x_abc1.jpg
000001_x_efg1.jpg
000001_x_hij1.jpg
000002_x_abc1.jpg
000002_x_efg1.jpg
000002_x_hij1.jpg
.
.
.
.
234562_x_abc2.jpg
234562_x_efg2.jpg
234562_x_hij2.jpg
Of theses files I have generated a 'list of files' that I need to pull out based on partial names i.e the numeric ID - first 6 numeric values in the file name e.g 234562*.jpg and copy them to a destination folder.
Note: Every numeric ID based search should give me 3 files and I need to copy all three. Any help would be appreciated.
I have tried the following code based on my search:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "DEST_DIR=my_desination"
SET "SEARCH_DIR=my_source"
FOR /F "tokens=*" %%a IN (%~dp0my_list.txt%) DO (
FOR /R "%SEARCH_DIR%" %%f IN (*%%a*) DO (
SET "SRC=%%~dpf"
SET DEST=!SRC:%SEARCH_DIR%=%DEST_DIR%!
xcopy /S /I "%%~f" "!DEST!"
)
)
And my list file is as below:
002631_*.jpg
054741_*.jpg
054992_*.jpg
055053_*.jpg
055054_*.jpg
055118_*.jpg
055267_*.jpg
055294_*.jpg
055382_*.jpg
055415_*.jpg
055466_*.jpg
055546_*.jpg
This is an example of copying specific files to a folder.
#ECHO OFF
set "SOURCE_DIR=%userprofile%\Desktop\Source"
set "DEST_DIR=%userprofile%\Desktop\Output"
set "FILENAMES_TO_COPY=test.txt test1.txt test2.txt"
pushd "%SOURCE_DIR%"
for %%F IN (%FILENAMES_TO_COPY%) do (
echo file "%%F"
xcopy /Y "%%F" "%DEST_DIR%\"
)
popd
pause
The script copies test.txt test1.txt and test2.txt from the folder Source, to the folder Output
Check out this page
Supposing the list file contains full file patterns one per line, the following should work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "SOURCEDIR=D:\Data"
set "TARGETDIR=D:\BackUp"
set "LISTFILE=D:\files.lst"
cd /D "%SOURCEDIR%" || exit /B 1
for /F "usebackq delims= eol=|" %%L in ("%LISTFILE%") do (
for /F "delims= eol=|" %%F in ('dir /B "%%L"') do (
copy "%%~F" "%TARGETDIR%\%%~nxF"
)
)
endlocal
exit /B
(edited to reflect additional information in the question)
for /f %%a in (partial.txt) do copy %%a "x:\destination folder\"
for every entry in the textfile copy <entry from textfile> to the destination.
see for /? for more details

Batch file for creating folders based on filenames, and moving files

I got a bunch of pictures taken with my camera in a parent folder. The filenames has this format; 'yyyymmdd_ttmmss.jpg', eg '20151008_0730.jpg'.
I want to create folders based on the 'yyyymmd'-part of the filename, but with the format 'yyyy-mm-dd'. So the file '20151008_0730.jpg' is moved into a folder named '2015-10-08'. This is what I got so far:
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\temp"
PUSHD %sourcedir%
FOR /f "tokens=1*" %%a IN (
'dir /b /a-d "*_*.jpg"'
) DO (
MD %%a
MOVE "%%a %%b" .\%%a\
)
POPD
GOTO :EOF
But I don't know how to format the %%a-variable before creating the folder.
(This is a similar question asked before, but not with creating the folder on this format)
#echo off
setlocal
copy nul "%tmp%\20151008_0730.jpg"
copy nul "%tmp%\20151009_0731.jpg"
copy nul "%tmp%\20151010_0732.jpg"
set "sourcedir=%tmp%"
pushd %tmp%
for /f "delims=" %%a in ('dir /b /a-d "*_*.jpg"') do (
set "file=%%a"
setlocal enabledelayedexpansion
set "folder=!file:~0,4!-!file:~4,2!-!file:~6,2!"
echo:
rem check if folder already exist
if not exist "!folder!\nul" echo md "!folder!"
echo move "!file!" "!folder!"
endlocal
)
popd
exit /b 0
output:
md "2015-10-08"
move "20151008_0730.jpg" "2015-10-08"
md "2015-10-09"
move "20151009_0731.jpg" "2015-10-09"
md "2015-10-10"
move "20151010_0732.jpg" "2015-10-10"
You have to clean up this sample by changing sourcedir and pushd, removing echo in front of each commands, also copy nul "....".
I used here VarSubstring and delayedexpansion

Windows batch file: converting all files in subfolders

I need to write a batch file to convert every file in every subfolder. I came up to the following solution:
set INDIR=<some path>
set OUTDIR=<some path>
mkdir "%OUTDIR%"
for /f "tokens=1*delims=." %%f in ('dir %INDIR% /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one . %%f %%g
)
)
for /f "delims==" %%d in ('dir %INDIR% /ad /b') do (
rem %%d is relative path
mkdir %OUTDIR%\%%d
for /f "tokens=1*delims=." %%f in ('dir %INDIR%\%%d /b /a-d') do (
rem %%f is file name
rem %%g is extension
call convert_one %%d %%f %%g
)
)
The problem is that it iterates through the first level subfolders only. When I add /s key to the dir command it returns full pathes instead of relative.
How can I improve the script to process all levels subfolders?
PS: I need separate values for relative path, file name and extension.
You don't show us convert_one - but here's a few clues...
for /f "delims=" %%f in ('dir %INDIR% /b /s /a-d') do (
rem %%~dpnf is file name and path
rem %%~dpf is file absolute path
rem %%~nf is file name-part
rem %%~xf is extension
call convert_one "%%~dpf" "%%nf" %%~xf
)
)
See for /?|more from the prompt for more...
(within your subroutine, %~n for n=1..9 strips quotes if needed - applying quotes means that "strings containing spaces" are regarded as a single string.
- maybe make your destination directory there...?)
#echo off
setlocal enableextensions disabledelayedexpansion
set "INDIR=%cd%"
set "OUTDIR=..\out"
subst :: /d >nul 2>&1 & subst :: "%INDIR%"
for /r "::\." %%a in (*) do (
if not exist "%OUTDIR%%%~pa" md "%OUTDIR%%%~pa"
call convert_one ".%%~pa" "%%~na" "%%~xa"
)
subst :: /d
This operates creating a subst drive, so the root of the drive point to the in folder. Then iterates over the folder structure recreating the structure in the output folder and calling the subroutine with the indicated parameters

Batch remove parenthesis from file name

After successfully removing a bunch of Google Drive Folder duplicates, some files retain a "filename(2)"name.
Is there a way to batch rename every file so the parenthesis and the number inside the parenthesis is gone?
That includes folders and sub-folders.
Try like this :
Create a file test.bat with the code below in it and replace the path to test in the var $path
#echo off
set $path="C:\Users\CN Micros\Desktop\PROGRAMMATION\test"
for /f "tokens=1-3 delims=^(^)" %%a in ('dir /b/a-d %$path%') do (
if exist %$path%\"%%a(%%b)%%c" echo ren %$path%\"%%a(%%b)%%c" "%%a%%c"
)
pause
Then run it in the CMD or by double clicking.
If the output is ok for you remove the echo
The program create 3 tokens : %%a = what's before the (), %%b What's inside the () and %%c what's after the ().
Then we arrange this 3 tokens to rename the files without the ().
If you have some file who have the same final name ie : "file(1)name", "file(2)name" --> "filename"
It will work only with the first one. If you have this case you have to add a counter at the end of file to be sure that they will be renamed.
This will create renfiles.bat.txt for you to examine in Notepad and then rename to .bat and execute if you are happy with it.
#echo off
dir /b /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl "(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Edit: This version will recurse through subdirectories:
#echo off
dir /b /s /a-d *(*).* |find /i /v "%~nx0" |find /i /v "repl.bat" |repl ".*\\(.*)\(.*\)(\..*)" "ren \q$&\q \q$1$2\q" xa >"renfiles.bat.txt"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "newname=!name:)=!"
SET "newname=!newname:(=!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
) ELSE (ECHO(REN "%%a" "!newname!%%~xa")
)
endlocal
)
GOTO :EOF
You'd need to set your required directory into sourcedir. I used u:\sourcedir which suits my testing.
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.

Resources