I have been using this site for sometime and have found it quite valuable in finding solutions to scripting issues. Today I resolve to ask a question as I have not found my solution after exhausting 3 days here, on Superuser, and Googling. So much for all about me, now my point...
I have several installs of a program, each slightly different version or flavor. Each install has a text file defining variables for its use. One of these variables defines a folder for user specific settings, USERSET=%APPDATA%\foo1\foo2. Overtime these user settings can become corrupt. I want to be able to delete these folders with a batch file or script. In short, I want to search a directory for text files that contain a specific string which sets a variable that points to a folder, get the value of that variable and use it to remove a directory.
This is what I have so far:
for /f "tokens=2 delims==" %%G in ('dir /b /s c:\foo\*.txt ^| findstr /i /f:/ userset') do rd /s /q %%G
The output looks like this:
H:\>rd /s /q %appdata%\foo1\foo2\USERSET
The system cannot find the file specified.
Apparently %appdata% does not expand?
Of course if I just type rd /s /q %appdata%\foo1\foo2\USERSET at the command prompt, it works.
I'm open to other languages, I am mostly familiar with Windows command line.
This is because environment variable expansion is done at parse time of a statement. While your for loop is running and %%G actually gets a value it's too late; variables have already been expanded.
You can work around this with a subroutine, I guess:
pushd C:\foo
for /f ... %%G in (...) do call :process "%%G"
popd
goto :eof
:process
call set "X=%~1"
rd /s /q "%X%"
goto :eof
I'd simply insert a CALL before the RD
....CALL RD ...
although I'd also try
....CALL ECHO(RD ...
first to verify - just for safety's sake.
Related
I am looking to make a batch file which will sift through a directory full of computer backups. The file format is "computername-date." Since I know the computer name is static, I need to find and take that directory so I can restore it's contents.
I never realized that for loops are so foreign from what I play with in other languages, so I find myself getting nowhere anytime soon.
REM First mount the drive that contains the backed up files
net use P: \\DC1\Shared\1Backups
REM Get the computer's name so we know what PC backup to use.
set SaveDirectory=%computername%
REM For each folder in the directory, do this when the computer name is found.
FOR /R "P:\" %%G in (%SaveDirectory%*) DO (
REM Restore files
echo found it.
REM Copy subdirectories into User Folder
mkdir P:\UGH
)
REM Dismount drive
The problem with what I have now is that when I run the code, the DO never runs. It should find that there is a folder called "INTERN-6.21.2019" by searching "INTERN*"
My impression of the for statement may be wrong. I need it to search through the P:/ Directory, not the subfolders. Compare the folder names to the SavedDirectory, then do something when they match. What am I doing wrong?
I've normally had good results with using CALL statements to invoke a subroutine rather than running items inside ( ).
The trick here is that you need to pass arguments to the the subroutine. Typically you'd pass the variable(s) you use in your FOR statement. These are referenced using %1 (or %1 %2 %3 if you have multiple things to pass) because these act like command line arguments. To exit the subroutine you then jump to EOF which then returns control to your FOR loop. After your FOR loop, don't forget to jump over your subroutine or you'll execute it again. In the example below I jump to :end, but you certainly could jump to somewhere else to do more things.
So using this methodology, your code might be like this:
set SaveDirectory=%computername%
FOR /R "P:\" %%G in (%SaveDirectory%*) DO CALL :process %%G
Goto :end
:process
REM Processing goes here
goto :end
:end
Hope this helps
This might get you going. This will find the "newest" directory. It is not clear from the question exactly what is to be copied and to where.
SET "MOSTRECENT=unknown"
FOR /F "delims=" %%d IN ('DIR /B /A:D /O:D "P:\%COMPUTERNAME%*"') DO (
SET "MOSTRECENT=%%~fd"
)
ECHO The most recent backup is "%MOSTRECENT%"
IF EXIST "MOSTRECENT" (COPY ...
This answer is based upon my understanding of what you're trying to do, copy a network directory named with the computername and a datestring, (with todays date), to another location:
#PushD "\\DC1\Shared\1Backups" || GoTo :EOF
#For /F "EOL=DTokens=1-3" %%A In (
'WMIC Path Win32_LocalTime Get Day^,Month^,Year'
)Do #For /F Tokens^=* %%D In ("%%C"
)Do #XCopy "%COMPUTERNAME%-%%B.%%A.%%D" "P:\UGH\" /H /R /K /Y>NUL 2>&1
#PopD
I'm trying to rename .jpg files which is in one of many subdirectories of e:\study\pubpmc\test\extracted.
I want to rename files to LastFolderName_ImageName.jpg.
(For example if Figure1.jpg is in e:\study\pubpmc\test\extracted\folder1
I want it to be renamed like this: folder1_Figure1.jpg)
So I need to take out the last folder name from the file's path.
Since it's my first time with batch scripting, I'm having a hard time.
I googled and made code similar to it
but it doesn't seem to work out.
Can you help me with it and tell me where I've done wrong?
Thank you! :)
#echo off
cd /D "e:\study\pubpmc\test\extracted"
for /r %%f in (*.jpg) do (
set mydir=%%~dpf
set mydir=%mydir:\=;%
for /f "tokens=* delims=;" %%i in (%mydir%) do call :LAST_FOLDER %%i
goto :EOF
:LAST_FOLDER
if "%1"=="" (
#echo %LAST%
goto :EOF
)
set LAST=%1
SHIFT
goto :LAST_FOLDER
)
JosefZ explains the obvious problems with your code, but he failed to point out a subtle problem, though his code fixed it:
FOR /R (as well as the simple FOR) begin iterating immediately, before it has finished scanning the disk drive. It is possible for the loop to reiterate the already named file! This would cause it to be renamed twice, giving the wrong result. The solution is to use FOR /F with command 'DIR /B', because FOR /F always processes the command to completion before iterating.
JosefZ also provides code that works for most situations. But there is a much simpler solution that works always:
#echo off
for /f "delims=" %%A in (
'dir /b /s /a-d "e:\study\pubpmc\test\extracted\*.jpg"'
) do for %%B in ("%%A\..") do ren "%%A" "%%~nxB_%%~nxA"
The "%%A\.." treats the file name as a folder and walks up to the parent folder. So %%~nxB gives the name of the parent folder.
The command could be run as a long one liner, directly from the command line (no batch):
for /f "delims=" %A in ('dir /b /s /a-d "e:\study\pubpmc\test\extracted\*.jpg"') do #for %B in ("%A\..") do #ren "%A" "%~nxB_%~nxA"
Avoid using :label and :: label-like comment inside (command block in parentheses). Using any of them within parentheses - including FOR and IF commands - will break their context.
Using variables inside (command block in parentheses). Read EnableDelayedExpansion: Delayed Expansion will cause variables to be expanded at execution time rather than at parse time [and CLI parses all the (command block in parentheses) at once]
Next script should work for you. Note rename statement is merely echoed for debugging purposes.
#ECHO OFF >NUL
SETLOCAL enableextensions disabledelayedexpansion
set "fromFolder=e:\study\pubpmc\test\extracted"
rem my debug setting set "fromFolder=D:\path"
for /F "tokens=*" %%f in ('dir /B /S /A:D "%fromFolder%\*.*"') do (
set "mydir=%%~ff"
set "last=%%~nxf"
call :renameJPG
)
#ENDLOCAL
goto :eof
:renameJPG
rem echo "%mydir%" "%last%"
for /f "tokens=*" %%i in ('dir /B /A:-D "%mydir%\*.jpg" 2^>nul') do (
echo ren "%mydir%\%%~nxi" "%last%_%%~nxi"
)
goto :eof
Resources:
SETLOCAL, disableDelayedExpansion, ENDLOCAL etc.
An A-Z Index of the Windows CMD command line
Windows CMD Shell Command Line Syntax
I already wrote a function for that. You give it any path and it returns you only it's filename or pathname. Works for any path: Url, Windows path, Linux path, etc...
Copy this function at the end of your batch script: (Instructions below)
rem ===========================================================================
:Name_From_Path
SetLocal
set _TMP_FOLDERNAME=%1
for %%g in ("%_TMP_FOLDERNAME%") do set _TMP_FOLDERNAME=%%~nxg
EndLocal & set _Name_From_Path=%_TMP_FOLDERNAME%
goto :EOF
rem ===========================================================================
Usage:
CALL :Name_Of_Path e:\study\pubpmc\test\extracted\folder1
ECHO %_Name_From_Path%
Result: folder1
If your program or com file traverses these folders when renaming, then it should be able to get the present working directory ( path ), pwd. You may be able to chop everything but the LAST_FOLDER out of this by also creating a PREVIOUS_FOLDER and doing a string replacement.
Or you may be able to break the folder names at the '\' token from the pwd into an array and use a -1 array reference to get the last folder name.
In any circumstance you'll want to check for a present working directory command.
If your creating a large text list of all these and issuing a single call to the batch file.
Then you may be better off with something like:
(Symmantic code warning )
(copy) /folderbase/some_folder/oneormore1/image_from_oneormore1.jpg (to) /folderbase/some_folder/oneormore1/oneormore1_image_from_oneormore1.jpg
Instead of copy, window uses rename, linux uses mv.
The latter example would require simply creating a duplicate list and replacing the \ with a _ while parsing through the tokens.
The code you've given is difficult to make sense of, so its hard to discern if you can simple concatenate the current folder and image name (stringify) and then write or rename them where they are.
A program is writing flatfiles on a certain directory, the problem is that another program is moving the file before it finishes the writing the flatfile. i have no control on both programs i can only set the path/directory for both so i decided to create a batch file that moves the files when "~" is not in the filename. since temporary files have "~" in their filenames. can you tell me what's wrong with my program?
echo off
%%a%% = Dir /b|Find /V "~" (C:\source)
move %%a%% (C:/dest)
pause
Any suggestion wouldbe helpful
Thanks!
I tried noodles code
for /f %%A in ('dir /b^|findstr /i /v /c:"~"') do move %%A (C:\dest).
error is
the filename,directory name or volume label syntax is incorrect
im pretty sure the C:\dest exist
for /f %%A in ('dir /b^|findstr /i /v /c:"~"') do echo %%A
See for /?, dir /?, and findstr /?.
Seems to be an XY problem to me.
Sadly, your valiant attempt at a solution is more error than not.
Returning to the actual problem - we have a file being created which needs to be moved after it has been created.
The critical question would be - how do we know that creation is complete?
It is possible but not probable that the file is indeed created as a temporary file, then renamed when complete. It could be that the being-created filename contains ~ but I'd regard the assumption as highly unreliable. ~ occurs frequently in filenames, but very often in short filenames (the 'short' name for a filename that does not follow the 8.3 pattern).
The normal method for creating a file is to open it, write to it, then close it when complete. Might sound obvious, but it's also possible to re-open it and append more data then re-close it, but that would be a relatively rare approach.
In all probability, the open-write-close scenario is used by the creation application. The trick we can use in batch here is that it will appear to have a zero-length until it is closed, hence
for /f %%a in (*.*) do if %%~za neq 0 ECHO(move "%%a" "c:\dest\"
should work (note that \ is a directory-separator; / is a switch-indicator. Note also that the move command is merely echoed - remove the echo( to activate the move after verification)
If we have the open-write-close-reopen-write_again-close scenario, it becomes more complicated. What we would do then is take a directory list at intervals (say 5 sec or so) and compare it to the previous list. If the filelength of a file remains stable and >0 then that file would seem ready to transfer, if not then wait for the next dir snapshot.
Back to OP for verification/comment...
Launch this in the folder and see what the echo command writes to the console.
#ehco off
for /f "delims=" %%A in ('dir /b /a-d^|findstr /v "~" ') do echo move "%%A" "d:\folder"
I need to change a file name in a batch script.
Below is a sample I made
SET date = 20210803
SET job = 69187
cd "H:\arbortray foldher\"
for %%i in (*.txt*) do REN %%i randum_%job%-text-%date%%%i
It is not working; does nothing. I want it to change a specific file name from a generic version to one using the globally defined variables that are used through out the script. The file is already being moved from another program that makes the file into this folder. I can not include the variable in the file name at those steps. I want to include the commands as part of a larger script that does other things using the variables. Specifically, in this case I need the commands to rename the file from the generic version to one that includes variables defined earlier in the script. These variables change weekly.
The problems are:
A) Your variable names will have spaces in them.
B) The CD command needs a /d
C) The for in do has a bug which has to be worked around by changing the extension and restoring it later.
#echo off
SET date=20210803
SET job=69187
cd /d "H:\arbortray foldher\"
for %%i in (*.txt) do REN "%%i" "randum_%job%-text-%date%%%~ni.tmp"
ren *.tmp *.txt
echo done
pause
The spaces caused the issue and in the rename command you need double quotes to cater for spaces.
take a look at the cross-platform renamer tool..
your example insecure:
date (isn't good practica)
md itsdir.txt
ren %UNICODECHARS% %insecure?%
answer:
#echo off
chcp 65001 >NUL 2>NUL.
set "v_date=20210803"
set "v_job=69187"
set "v_dir=%cd%"
for /f "tokens=* delims=" %%s in ('dir /b /a-d "%v_dir%" ^| findstr /i /e /c:".txt"') do #ren "%v_dir%\%%s" "randum_%v_job%-text-%v_date%%%s"
forgot about /d and cd ))
maybay you mean "randum_69187-text-20210803-example.txt" in example result?
I have a directory with the following structure:
C:\Directory1\
sub1\
sub2\
sub3\
somefilename.txt
someotherfile.txt
Inside each sub*\ there are .dat files that I need to copy to another directory mirroring along the way the directory name where they were found. So if I find C:\Directory1\sub2\file.dat I would copy that into C:\mirror\sub2\file.dat and so on.
I tried several combinations of things similar to
for /R %SRC_DIR% %%f in (*.dat) do copy "%%f" %BACKUP_DIR%\%%~nf%%~xf
(please note this is just an example of code I was playing with, i know it doesn't work)
anyway, after trying to a couple of day I still don't know how to do it. Any chance of help?
Code is appreciated.
thanks!
This works for me:
#echo off
setlocal EnableExtensions EnableDelayedExpansion
set SourceDir=c:\source\dir
set TargetDir=d:\target\path
set FileMask=*.cpp
for /r "%SourceDir%" %%F in (%FileMask%) do (
call :ReplacePrefix target_path "%%~F" "%SourceDir%" "%TargetDir%"
call :CopyFile "%%~F" "!target_path!"
)
endlocal
goto :EOF
:CopyFile %1=source_path %2=target_path
mkdir %~dp2
copy %1 %2
goto :EOF
:ReplacePrefix %1=result_var_name %2=string %3=replace_what %4=replace_with
rem a question mark is prepended to ensure matching only at the beginning of the string
set rp_value=?%~2
call :DoIt "set %1=%%rp_value:?%~3=%~4%%"
goto :EOF
:DoIt %1=cmd
%~1
goto :EOF
Keep in mind though that it can break if paths contain unusual characters (such as = and some others which I can't remember now).
Use the following XCOPY command:
xcopy "c:\directory1\*.dat" "c:\mirror\" /s /v /c /y
If you do not want to see the filenames displayed on the screen add '/q' to the list of options.
The '/s' will copy files from subfolders. If the subfolders don't already exist they will be created.
The '/v' forces verification. Not necessary but it's nice to have that peace of mind.
The '/c' forces XCOPY to continue with the rest of the files if it encounters any problems - in other words, your batch file won't halt abruptly with only 'some' of your files copied. XCOPY will copy all that it can.
The '/y' suppresses prompting to overwrite an existing file.