I am trying to copy from one directory to another, all files that have 2 digits format (ex. 12.txt, 15.pdf, 25.doc... etc), this is in Windows.
In Linux this works:
cp -t target_directory {10..99}.*
In Windows what will be the solution?
This is ugly but should work
for /L %i in (10,1,99) do #copy %i.* dest_folder_here >nul 2>&1
It tries every file in the range suppressing conditions. Adjust copy to overwrite if needed.
I would filter for the correct files by findstr, like this:
for /F "delims= eol=|" %%I in ('dir /B /A:-D "??.*" ^| findstr /X "[0123456789][0123456789]\.[^.]*"') do copy "%%~I" "\path\to\destination"
I avoided to use range expressions [0-9] because they might also match ², ³.
If you also want to match files with no file name extension, change the search string to [0123456789][0123456789] [0123456789][0123456789]\.[^.]*.
Related
I'm working with files with various extensions, such as .bmp, .txt, etc, and I'm in the process of sorting them around through a batch file. However there are some files without any extension that I would like to be able to specifically manipulate.
How can I target extension-less files alone? Such as moving/deleting them exclusively. Or moving files with extensions while ignoring extension-less files?
If you only want to view or process files with an extension, use this syntax:
dir /b /a-d [path] | findstr /l "."
Only files containing a dot will be displayed.
You can add this to the for statement to get the output to a processable variable.
Of course, you must add an escape character before the pipe character in the for sentence.
However, to perform the operation in the subdirectories as well, it is best to use the for sentence, as a dot may appear in the folder name. like this:
for /f "tokens=*" %d in ('dir /s /b /ad') do #dir /b /a-d "%d" | findstr /l "."
To do the opposite (showing only files without an extension) simply add the findstr /v parameter.
I would like to print all file names and sizes of files that are located in any folder that matches "alpha".
I understand dir /s will print all file names and sizes of the entire directory and sub directories recursively, but don't know how write a command to only display contents of folders which match a specific string (in my case alpha) in their file path.
for example if this is my directory:
C:\Users\raigovind93\Documents\1\alpha\doc1.txt
C:\Users\raigovind93\Documents\2\1\1\alpha\doc2.pptx
C:\Users\raigovind93\Documents\2\1\1\alpha\apple\doc3.xslx
I would like to print:
doc1.txt 20020 bytes
doc2.pptx 102002 bytes
doc3.xslx 289 bytes
And for clarity sake, if you can show both an answer that shows folders along with file names (to verify correctness of the command) and one without folders, that will be super helpful!
on command line:
for /r %a in (*) do #echo %a %~za bytes|find /i "\alpha\"
for use in batchfiles, double the percent-signs:
for /r %%a in (*) do #echo %%a %%~za bytes|find /i "\alpha\"
see for /? for details.
Use dir /B /S /A:-D to return all files (/A:-D) in the current working directory recursively (/S) as a bare list (/B), then pipe (|) the result into find /I "\alpha\" to filter for items that contain \alpha\ in their paths case-insensitively (/I); then parse its output by a for /F loop:
for /F "eol=| delims=" %F in ('dir /B /S /A:-D ^| find /I "\alpha\"') do echo %~nxF %~zF bytes
To include the full paths in the returned items, replace %~nxF by %~fF.
Remember to double the % signs to use this command line in a batch file.
Try using
dir /s |find /I "\alpha\"
You can use 'find' with the pipe on other commands too.
I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.
I am trying to rename a lot of files. I only want to change the extention from ".pdf.OCR.pdf" to ".pdf"
So far I got the following code
rem for /r myPDFfolder %%i in (*.pdf.OCR.pdf) do ren "%%i" "%%~ni.pdf"
But it does not appear to work with the extension that has multiple dots -- what am I doing wrong?
Extension is the part of file name after the last dot.
Use string replacement to strip the unneeded part:
setlocal enableDelayedExpansion
for /f "eol=* delims=" %%i in ('dir /s /b "r:\*.pdf.OCR.pdf"') do (
set "name=%%~nxi"
ren "%%i" "!name:.pdf.OCR=!"
)
P.S. Parsing of dir is used to make the code more robust in case a different text is stripped which might have changed the sorting order and cause for to process the file twice or more times.
There is no need for a batch file. A moderate length one liner from the command prompt can do the trick.
If you know for a fact that all files that match *.pdf.ocr.pdf have this exact case: .pdf.OCR.pdf, then you can use the following from the command line:
for /r "myPDFfolder" %F in (.) do #ren "%F\*.pdf.ocr.pdf" *O&ren "%F\*.pdf.o" *f
The first rename removes the trailing .pdf, and the second removes the .OCR. The above works because *O in the target mask preserves everything in the original file name through the last occurrence of upper-case O, and *f preserves through the last occurrence of lower-case f. Note that the characters in the source mask are not case sensitive. You can read more about how this works at How does the Windows RENAME command interpret wildcards?
If the case of .pdf.ocr.pdf can vary, then the above will fail miserably. But there is still a one liner that works from the command line:
for /r "myPDFfolder" %F in (*.pdf.ocr.pdf) do #for %G in ("%~nF") do #ren "%F" "%~nG"
%~nF lops off the last .pdf, and %~nG lops off the .OCR, which leaves the desired extension of .pdf.
You should not have to worry about a file being renamed twice because the result after the rename will not match *.pdf.ocr.pdf unless the original file looked like *.pdf.ocr.pdf.ocr.pdf.
If you think you might want to frequently rename files with complex patterns in the future, then you should look into JREN.BAT - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward. Full documentation is embedded within the script.
Assuming JREPL.BAT is in a folder that is listed within your PATH, then the following simple command will work from the command line, only renaming files that match the case in the search string:
jren "(\.pdf)\.OCR\.pdf$" $1 /s /p "myPDFfolder"
If you want to ignore case when matching, but want to force the extension to be lower case, then:
jren "\.pdf\.ocr\.pdf$" ".pdf" /i /s /p "myPDFfolder"
Alternative solution, without delayed expansion (remove ECHO to actually rename any files):
#echo off
rem iterate over all matching files:
for /F "delims=" %%A in (
'dir /S /B /A:-D "myPDFfolder\*.pdf.OCR.pdf"'
) do (
rem "%%~nA" removes last ".pdf"
for /F %%B in ("%%~nA") do (
rem "%%~nB" removes ".OCR" part
for /F %%C in ("%%~nB") do (
rem "%%~nC" removes remaining ".pdf"
ECHO ren "%%~fA" "%%~nC.pdf"
) & rem next %%C
) & rem next %%B
) & rem next %%A
NOTE: The directory tree is enumerated before for iterates through it because otherwise, some items might be skipped or tried to be renamed twice (see this post concerning that issue).
how can i modify filenames in a folder based on delimiters in the filename?
I have a folder with images that get picked up by a different program based on a schedule- the program can only analyze the images if it contains just the main name (sku#) not the additional data that the photographers add after the name
using the command line can i run some sort of script to modify the filenames & delete all characters from after an underscore or hyphen (also need to delete underscore or hyphen)
(i dont know if & how its possible to do this thru the windows command line but i do have the option of running such a 'script' in cygwin- I prefer to use whatever works best...)
I haven't had a need to do this: but with a quick search I found this link on another Stack Exchange site.
A few people uploaded some powershell scripts. The top answer is a GUI tool to do mass name-changes.
There is this CMD example:
dir /B > fileList.txt
for /f "tokens=1,2,3" %i in (fileList.txt) DO ren "%i %j %l" %l
The first line outputs the list of files into a file called fileList.txt. The second line separates each of the names in the list into 3 parts, the #, the "-" and the rest of the name. For each of those it does the rename command.
#ECHO OFF
SETLOCAL
FOR /f "delims=" %%a IN ('dir /b /a-d ^|findstr "_ -"') DO (
FOR /f "delims=_-" %%i IN ("%%a") DO ECHO REN "%%a" "%%i%%~xa" 2>nul
)
FOR /f "delims=" %%a IN ('dir /b /a-d ^|findstr "_ -"') DO ECHO failed ON "%%a"
This should do the job.
First step is to perform a directory listing in /b basic form (without headers - names only) and /a-d without directory names. This is filtered by findstr on (either underscore or dash) and %%a acquires each filtered filename in turn.
The next step is to take those filenames and split them into the parts on either _ or -. The first token (up to, but not including the delimiter) is applied to %%i, so the rename required is from "%%a" (the original filename) to "%%i%%~xa" - the first token+the extension from the original filename.
It's quite possible that the attempt to rename will fail because the new name already exists, so the error message is simply ignored (2>nul)
Finally, look for any un-renamed files and report them (optional, of course)
Note that the REName command is merely ECHOed. This ensures nothing is actually changed while you verify this is what you want to do. Simply remove the ECHO before the REN to activate the rename.