"for /f" doesn't set variable using cmd.exe batch file - windows-xp

rmtshare \\server\sharename$ | for /f "tokens=3,4,5 delims=\" %%A in ('find "Path"') DO
SET path1=%%A\%%B\%%C
echo %path1%
It will do everything up to the set area. I want to take out the physical path of the share and turn it into a variable to be used later. My approach is not working, I don't know what I am missing.

Make sure that the command you want to run in the FOR loop is on the same line (e.g. for /f ... do set path1=%%A\%%B\%%C or, if you have multiple commands, that they are grouped in parenthesis, like so:
for /f ... do (
echo "Setting path1"
set path1=%%A\%%B\%%C
)
...
(Also, do you have SETLOCAL EnableDelayedExpansion set? If so, you may need to use !path1! instead of %path1%.)

I believe SET is not persistent, you should use SETX to set a variable in command prompt and have it stay outside that CMD session,
afaik, SETX is native on windows 7, but is available on resource kits for older windows versions.

You need to include usebackq in your options like so:
for /f "usebackq tokens=3,4,5 delims=\" %%A in ...

Related

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!"
)

Batch Code explanation

I found this code online, and it allows me to choose a folder using a GUI. Can someone please explain to me how this works, and how I can get an output from it. I am hoping I can get an output and assign that to a variable.
NOTE: I did NOT make this. I simply found it online at another stack overflow post.
:: fchooser.bat
:: launches a folder chooser and outputs choice to the console
:: http://stackoverflow.com/a/15885133/1683264
#echo off
setlocal
set "psCommand="(new-object -COM 'Shell.Application')^
.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
for /f "usebackq delims=" %%I in (`powershell %psCommand%`) do set "folder=%%I"
setlocal enabledelayedexpansion
echo You chose !folder!
endlocal
Thanks a lot,
ChapelCone56
set "psCommand="(new-object -COM 'Shell.Application')^.BrowseForFolder(0,'Please choose a folder.',0,0).self.path""
It is creating a new powershell object for windows to invoke the browse folder dialogue
FOR /F
Loop command against the results of other command.
usebackq
use the different quoting style
powershell %psCommand%
creates a pipileline that another object can use
set "folder=%%I"
setting folder variable to the choosen folder name
echo You chose !folder!
display the choice
if you want to use the selected folder name, use variable folder whose value can be found as !folder!

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.

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

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.

MS DOS edit a file

I am writing a batch script which I wish to open a file and then change the second line of it. I want to find the string "cat" and replace it with a value that I have SET i.e. %var% . I only want this to happen on the second line (or for the first 3 times). How would you go about doing this?
I just solve it myself. It will lookup var on line two only.
#echo OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET filename=%1
set LINENO=0
for /F "delims=" %%l in (%filename%) do (
SET /A LINENO=!LINENO!+1
IF "!LINENO!"=="2" ( call echo %%l ) ELSE ( echo %%l )
)
But I prefer using cscript (vbscript or even jscript).
First of all, using a batch file to achieve this, is messy (IMHO). You will have to use an external tool anyway to do the string replacement. I'd use some scripting language instead.
If you really want to use a batch, this will get you started.
This would be ugly to do with native batch scripting. I would either
Do this in VBScript. If you really need this in a batch file, you can call the VBScript file from the batch script. You can even pass in %var% as an argument to the VBScript.
Use a sed script. There are windows ports of Unix commands like GnuWin32, GNU Utilities for Win32 (I use these), or Cygwin.
I would create a script that would:
scan the input file
write to a second output file
delete the input
rename the output
As far as the dos commands to parse, I did a Google Search and came up with a good starting point:
#echo off
setlocal enabledelayedexpansion
set file=c:\file.txt
set output=output.txt
set maxlines=5000
set count=0
for /F "tokens=* usebackq" %%G in ("%file%") do (
if !count!==%maxlines% goto :eof
set line=%%G
set line=!line:*000000000000=--FOUND--!
if "!line:~0,9!"=="--FOUND--" (
echo %%G>>"%output%"
set /a count+=1
)
)
(Stolen from teh Intarwebnet)