Batch file : copy all file except those its name contain some substring - command-line

first of all im beginner. i want to create batch file to search through specific folder (including all it subfolder) and copy all file inside it except those which filename contain some specific string,this is what i have so far
set now=fish
set logDirectory="C:\Users\paiseha\Desktop\bb\"
for /r %logDirectory% %%i IN (*%now%*.*) do (
rem copy process goes here
)
let say i have 3 file in it
C:\Users\fareast\Desktop\bb\one.txt
C:\Users\fareast\Desktop\bb\twofishtwo.txt
C:\Users\fareast\Desktop\bb\three.txt
so i want to copy file one.txt and three.txt only, but instead it copy only the second one,i know its because of *%now%*.* so how can i invert it so that it does the other way around, help me pls, thanks in advance

try:
#ECHO OFF &setlocal
set "now=fish"
set "logDirectory=C:\Users\paiseha\Desktop\bb"
for /f "delims=" %%a in ('dir /a-d/b/s "%logDirectory%"^|findstr /riv "^.*[\\][^\\]*%now%[^\\]*$"') do (
rem copy process goes here
)
EDIT: The \ character is represented as [\\] instead of \\ because of a quirk on how Vista FINDSTR regex escapes \. Vista requires \\\\, but XP and Win 7 use \\. The only representation that works on all platforms is [\\]. See What are the undocumented features and limitations of the Windows FINDSTR command? for more info.

for /f "delims=" %%a in ('dir /a-d/s/b "%logDirectory%" ') do echo %%~nxa|findstr /i /L "%now%" >nul&if errorlevel 1 ECHO COPY "%%a"
should work for you.

Related

Trying to rename files based on file size

I am trying to rename some MP4 files based on file size of mp4 files in another directory. I want to name all files with identical sizes to same name. Meaning if the file size of the source file matches the size of file in the comparison directory, the source file is renamed to whatever the compared file is named. Because both directories need to be read recursively I'm thinking it would be easier to make a list for comparison with the info in it in 2 columns by using the DIR /s /b echo %%~zs>>filesizelist.txt command giving me a list like
123456789 movie.mp4
987654321 movie2.mp4
Then pass all source mp4s to the batch file and if %%~za matches a value in first column then ren the file to the
corresponding filename. Is this the best path? I tried to script it to work on the fly and that was both a no-go and the source of my 3 day headache(plus the reference list rarely changes and is obviously easily updated). Can someone please assist me with the script?
I do some test with my mp4, and the code works, and for you perform your test, you w´ll need change/put this 2 line above with the path to your folder/directory (one to keep and another to compare), by replacing in the line code is like this:
`set "_target_to_keeped=C:\Users\ecker\Videos\Target"`
`set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"`
You need add the folder where are files to keep and files to rename (if size+name match) on same lines where are the 2 lines code up in this test (sorry not explain in good English, my English is not help me). By now, is late 01:53, i need sleep... yep! so, have nice code!
#echo off && setlocal enableextensions enabledelayedexpansion
cd /d "%~dp0"
set /a _cnt_in_looping= 1 - 1
set /a _cnt_files_size= 1 - 1
set "_target_to_keeped=C:\Users\ecker\Videos\Target"
set "_target_to_rename=C:\Users\ecker\Videos\Ren_it"
cd /d "!_target_to_keeped!"
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4" 2^> nul ^| find "" /v /c') do set _cnt_in_looping=%%i
for /f "tokens=* delims=^ " %%i in ('^<nul dir /o-d /on /b "*.mp4"') do (
set "_file_now_keep=%%i"
set "_file_now_keeped=%%~zi %%i"
call :_to_compare_:
)
set /a _total_files_renamed=!_cnt_in_looping! - !_cnt_files_size!
set /a _total_files_n_chang=!_total_files_renamed! - !_cnt_in_looping! * -1
echo/Total of files renamed = !_total_files_renamed!
echo/Total of files n chang = !_total_files_n_chang!
endlocal
goto :_end_of_file_:
:_to_compare_:
if not exist "!_file_now_keep!" exit /b
for /f "tokens=*" %%I in ('^<nul dir /o-d /on /b "!_file_now_keep!"') do (
set "_file_now_compare=%%~zI %%I"
set "_path_now_compare=%%~dpI"
if "!_file_now_compare!" == "!_file_now_keeped!" (
rename "!_path_now_compare!\%%I" "%%~zI %%I"
echo/ rename "%%~I" "%%~zI %%I"
if ["!errorlevel!"]==["0"] call set /a _cnt_files_size=!_cnt_files_size! + 1
timeout /t 10
)
)
exit /b
:_end_of_file_:

Rename only a specific part of every filename in a folder

I want to, for example, replace all -v1 parts in every filename for -v2. It's almost working, but the access is denied because the files are being used by the cmd process itself during execution. My current code is:
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do ren "%%a" "%%a:-v1=-v2"
Any suggestions? :)
EDIT:
I've also tried for /r %%i in ("*-v1*") do ren "%%~nxi" "%%~nxi:-v1=-v2" and it finds the files and uses the correct rename value, but still outputs that it can't access the file because it is in use by another process. I'm sure that process is the cmd.exe itself, because after I close the command prompt, I can change the filenames manually without any problems.
I also tried by writing the current filenames to a temporary .txt file with the idea that the files I want to rename aren't used by any command. Then read the file with the type command within a for loop to rename each file, but same thing.
It's quite frustrating, any help is appreciated :)
substring substituion doesn't work with for variables (%%a). Use a "normal" variable and delayed expansion:
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('dir /a-d /b *-v1*') do (
set filename=%%a
ren "%%a" "!filename:-v1=-v2!"
)

rename multiple files in order with command prompt

I have some files with different names.
Leviathan.txt,Dragon.txt and so on
I wanted to turn it into a digit begins
1.txt,2.txt,3.txt,4.txt and so on
how to perform like other language by using For and function that can pass amount files in folder?
my code so far i know is dir and ren. and i stuck now.
ren *.txt 1.txt
Next code snippet could work for you (save with .bat extension); note that rename command is echoed merely for debugging purposes:
#echo off
SETLOCAL enableextensions enabledelayedexpansion
set /A "ii=0"
pushd "working_directory_here"
for /F "delims=" %%G in ('dir /B /ON "*.txt" 2^>NUL') do (
set /A "ii+=1"
echo ren "%%~G" "!ii!%%~nxG"
)
popd
If you insist on an one-liner (launch in proper working directory):
cmd /E:ON /V:ON /K (#echo off^&set /A "ii=0" ^>NUL^&for /F "delims=" %G in ('dir /B /ON "*.txt" 2^^^>NUL') do (set /A "ii+=1" ^>nul^&echo ren "%~G" "!ii!%~nxG"))^&exit
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~G etc. special page) Command Line arguments (Parameters)
(EnableDelayedExpansion) Setlocal EnableDelayedExpansion
(^>, %% etc.) Syntax : Escape Characters, Delimiters and Quotes
Assuming none of your existing files are already named something like n.txt, where n is a number, then simply CD to your folder, and run the following command from the command line:
for "tokens=1* delims=:" %A in ('dir /b *.txt^|findstr /n "^"') do #ren "%B" "%A.txt"
Double up the percents if you use the command within a batch script.
EDIT
I forgot about my JREN.BAT utility - a regular expression renaming utility. It is pure script (hybrid JScript/batch) that runs natively on any Windows machine from XP onward.
JREN has a built in ability to incorporate a number into each new file name, and as an added bonus, it can left pad the number with zeros so that a DIR command lists the files in numerical order. The default numeric width is 3 digits, so files would be like "001.txt", "002.txt', ... "010.txt", ... "100.txt", etc.
jren "^.*" "$n+'.txt'" /j /fm *.txt
The /NPAD option specifies the minimum numeric width, so NTAB 1 produces no padding, which is what the original question asked for.
jren "^.*" "$n+'.txt'" /j /fm *.txt /npad 1
Since JREN is a batch script itself, you must use CALL JREN if you put the command within another batch script.
Full documentation is available from the command prompt via jren /? | more. My console window is configured with a large buffer, so I can scroll back to see prior output, and I don't bother with piping the help to MORE.

Replace text within a text file with Windows command line

Without being able to install all sorts of fancy find and replace tools, I need to find and replace strings within text files with the command line from Windows Server 2008.
How would I do that?
Example:
text.md
Hello world!
Change to:
text.md
Hello everyone!
I'm looking for something like:
for /f %%A in (text.md) do (
set "line=%%A"
if defined line (
// and here the replacement
) ELSE echo.
)
Using repl.bat which you would put on the path (say in C:\Windows or a utility folder that you add to the path)
type "text.md"|repl "world" "everyone" >"text.tmp"
move /y "text.tmp" "text.md"
repl.bat is a SED-like helper batch file from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855
findrepl.bat is GREP-like helper batch file from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697
If you want plain batch techniques then it will depend on the exact task and text makeup.
This has worked for me:
(for /f "tokens=1,* delims=]" %%A in ('"type text.md|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:world=everyone%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)) >text2.md
move /Y text2.md text.md

eol unix to windows in command line .bat batch

I am trying to convert text file eol to windows format from unix on windows xp machine using command line (batch file). how do I do that? what is the command for that? thanks.
This simple script is fast and works great except it converts every TAB character into 8 spaces. The number of spaces can be modified with the MORE /T option, but there is no way to preserve the TAB characters. Pass the file name (optionally with path) as the one and only argument.
#echo off
more %1 >%1.new
move /y %1.new %1 >nul
All that is needed is to read and echo each line. The FOR /F command is perfect, except it ignores empty lines. Here I use FINDSTR to prefix each line with the line number, followed by a :, thus preserving empty lines. Then I use search and replace to remove the number prefix. I must toggle delayed expansion on and off within the loop to preserve any ! that may appear. This script preserves TABs, but is limited to ~8191 bytes per line. It is also relatively slow. It will become very slow with very large files.
#echo off
setlocal disableDelayedExpansion
>%1.new (
for /f "delims=" %%A in ('findstr /n "^" %1') do (
set "ln=%%A"
setlocal enableDelayedExpansion
echo(!ln:*:=!
endlocal
)
)
move /y %1.new %1 >nul
Finally, here is a hybrid batch/JScript solution that is very fast, and does not have any limitations that I am aware of.
#if (#X)==(#Y) #end /* Harmless hybrid line that begins a JScript comment
::************ Batch portion ***********
#echo off
<%1 cscript //E:JScript //nologo "%~f0" >%1.new
move /y %1.new %1 >nul
exit /b
************* JScript portion **********/
while (!WScript.StdIn.AtEndOfStream) {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine());
}