EDIT: Updated the Code with "Stephan" Edits. I tested some dummy files and everything seems to work. Thanks for taking the time out getting this fixed and working. I can now focus on moving forward on projects instead of getting outdated game development tools working. This was for the Source Engine SDK to help get game models loaded correctly with backwards compatibility.
"sodacan.dx90.dx80.vtx" should be "sodacan.dx80.vtx".
"sodacan.dx90.sw.vtx" should be "sodacan.sw.vtx".
ORIGINAL POST:
This batch file is suppose to restore missing vtx files by copying an existing vtx file. But the vtx files require a special prefix attached to them before the extension, such as "sodacan.dx80.vtx" and "sodacan.sw.vtx".
The Problem is that the script is not working as it should. The script is ran on a folder that contains VTX files with different names. The script is suppose to create copies of a single vtx file and name them. The example below shows what you start out with and the end result.
You start out with the original, such as
sodacan.dx90.vtx (Original File)
You should end with
sodacan.dx90.vtx (Original File)
sodacan.dx80.vtx (Copy 1)
sodacan.sw.vtx (Copy 2)
But I actually get this from the code.
awning001a.dx90.vtx (Original File)
awning001a.dx90.sw.vtx (Copy 1)
awning001a.dx90.dx80.vtx (Copy 2)
These files work with there associated MDL File, such as (sodacan.mdl) but the script does not touch those files at all.
Below is the code in the batch file
#ECHO OFF
echo Initiating VTX re-construction...
set /a count=0
for /r %%F in (.) do (
Pushd %%F
for %%i in ("*.vtx") do (
for %%j in ("%%~ni") do (
echo Fixing "%%~ni.mdl".
if not exist "%%~nj.dx80%%~xi" copy "%%i" "%%~nj.dx80%%~xi" && set /a count+=1
if not exist "%%~nj.sw%%~xi" copy "%%i" "%%~nj.sw%%~xi" && set /a count+=1
)
)
popd
)
echo %count% reconstructed VTXes.
pause
Replace your for %%i loop with the following:
for %%i in ("*.vtx") do (
for %%j in ("%%~ni") do (
echo Fixing "%%~ni.mdl".
if not exist "%%~nj.dx80%%~xi" copy "%%i" "%%~nj.dx80%%~xi" && set /a count+=1
if not exist "%%~nj.sw%%~xi" copy "%%i" "%%~nj.sw%%~xi" && set /a count+=1
)
)
and at the end use:
echo %count% reconstructed VTXes.
Changes to your code:
process only the desired extension instead of processing all files and compare the extension with an if
Check if the destination file exists before copying
increment the counter only, if there was a copy
take care of the proper filenames (two dots) with another for %%j (effectively removing two "extensions")
adapted the last echo line to include the counter
(and don't forget to popd at the end of your for /R loop - there is just a limited stack for pushd and without popd you might run into that limit, generating an errormessage and exiting the script)
Related
I have a custom service that automatically generates files every 60 mins into a particular directory with part of the filename incrementing numerically, Eg:
File_00004.job
File_00003.job
File_00002.job
File_00001.job
Currently I have an issue where on occasion a file isn't generated, which results in gaps in the file sequence. This issue then causes a number of issues if not identified ASAP.
I'd like a batch file to identify if I have a gap in the file name sequence.
Tried looking for solutions from existing posts, but haven't found something that fits, so apologies if this has been covered elsewhere.
#ECHO OFF
SETLOCAL enabledelayedexpansion
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "mask=file_??????.job"
SET "lowest="
SET "highest="
FOR /f "delims=" %%a IN (
'dir /b /a-d /on "%sourcedir%\%mask%" '
) DO (
IF NOT DEFINED lowest SET "lowest=%%~na"
SET "highest=%%~na"
)
SET "lowest=%lowest:*_=1%"
SET "highest=%highest:*_=1%"
ECHO checking range %lowest:~1% to %highest:~1%
:: See whether an entry in the range is missing; report&create an empty file if so.
FOR /L %%a IN (%lowest%,1,%highest%) DO SET "name=%%a"&SET "name=file_!name:~1!.job"&IF NOT EXIST "%sourcedir%\!name!" echo !name! missing&(copy nul "%sourcedir%\!name!" >nul)
GOTO :EOF
Alternative structure for the for /L loop:
FOR /L %%a IN (%lowest%,1,%highest%) DO (
SET "name=%%a"
SET "name=file_!name:~1!.job"
IF NOT EXIST "%sourcedir%\!name!" (
echo !name! missing
copy nul "%sourcedir%\!name!" >nul
copy "d:\path to\template.file" "wherever\!name!" >nul
copy "d:\path to\template.file" "anotherplace\!name!" >nul
echo Batch is fun and powerful
copy "d:\path to\template.file" "a third place\!name!" >nul
)
)
The critical point is the positioning of the ( - must be directly after and on the same line as do or else or the logical comparison clause of if and must be matched by a ) (which doesn't need to be on its own line - I find it easier that way, to align indentation.) )s that are not intended to close a block need to be escaped with ^, thus: ^)
There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.
Coding is not my speciality but I have come across a problem and google search has guided me to using batch file process in solving it. Essentially I have a couple of thousands of files that need to be moved into folders and they have a very simple file structure, listed below:
UK--London--Filename.pdf
UK--London--Filename2.pdf
UK--Manchester--Filename3.pdf
UK--Liverpool--Filename4.pdf
UK--Chester--Filename5.pdf
I would like the script to:
1. Pick up the first "--" check if the folder exists, if not create it
2. Pick up the second "--" check if the folder exists, if not create it
3. As there might be more than two "--", ignore the rest
4. Move file into the subfolder
To that end, the output should be some like (note "FILETEST" is the folder I am using to test the script):
C:\FILETEST\UK\London\UK--London--Filename.pdf
C:\FILETEST\UK\London\UK--London--Filename2.pdf
C:\FILETEST\UK\Manchester\UK--Manchester--Filename3.pdf
C:\FILETEST\UK\Liverpool\UK--Liverpool--Filename4.pdf
C:\FILETEST\UK\Chester\UK--Chester--Filename5.pdf
I have had an attempt to re-using a script from another question in stackoverflow (Batch create folders based on part of file name and move files into that folder). I understand that it would not do exactly what I need, but cant seem to get any output.
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1*delims=--" %%a IN (
'dir /b /a-d *.*.*'
) DO (
ECHO MD %%a
ECHO MOVE "%%a.%%b" --\%%a\
)
POPD
GOTO :EOF
Apologies for any headaches caused, I am hoping this is a simple one to solve.
Thank you,
Panos
#ECHO OFF
SETLOCAL
SET "sourcedir=c:\FILETEST"
PUSHD %sourcedir%
FOR /f "tokens=1,2*delims=-" %%a IN (
'dir /b /a-d *--*--*.*'
) DO if "%%c" neq "" (
ECHO MD "%%a"
ECHO MD "%%a\%%b"
ECHO MOVE "%%a--%%b--%%c" ".\%%a\%%b\"
)
POPD
GOTO :EOF
Read the directory list of files in the current directory, (/a-d = no directorynames) that match *--*--*. Tokenise so that %%a acquires the part before the first --sequence, %%b the second and %%c the remainder.
If %%c is not empty then make the directories ".\%%a" and ".\%%a\%%b" (quoted because any spaces in the name would otherwise be seen as "create two directories") then move the file, again quoted for the same reason.
Note that each character individually between delims= and the close-quote is a delimiter - a delimiter-string is not supported. Consequently, this code will pick up - as well as --- and any other sequence of - and try to process it. You could gate the create/move further by adding if exist "%%a--%%b--%%c" directly after the if "%%c" neq ""before the (.
The md will create a directory if the target name does not already exist, and produce an error-message if it already exists. To suppress the error message, append 2>nul to the md lines.
I often have to track down files at work, pulling a hundred or so out of a list of a thousand, so I created a batch file that would copy the files in a location that are listed in a csv in the batch files directory and then paste them in a folder on my desktop.
This worked, but I often have to do this for hundreds of files across a dozen or so directories, so I tried to modify it to include subdirectories. I'm having an issue getting it to work and I was wondering if the excellent people at StackOverflow could help. Thanks!
#echo on
set theDest="%USERPROFILE%\Desktop\Batch Copies %date:/=%"
set /p MY_TEXT=<FileList.csv
echo.%MY_TEXT%
set MY_TEXT=%MY_TEXT:"",""=,%
echo.%MY_TEXT%
if not exist %theDest% md %theDest%
pause
for /R %MY_TEXT% %%G IN (.) do (
for /F "skip=1 delims=" %%f in (FileList.csv) do (
if exist "%MY_TEXT%\%%f.*" (
copy "%MY_TEXT%\%%f.*" %theDest%
) else (
if exist "%MY_TEXT%\%%f" (
copy /R "%MY_TEXT%\%%f" %theDest%
) else echo "%MY_TEXT%\%%f" does not exist && echo 0 file^(s^) copied. && echo ERROR: %%f not found && echo. && pause)
)
)
echo. && echo. && echo Process Completed. &&echo.
pause
Part of the problem is I was just getting into batch when I wrote this and then left it and am trying to come back and figure out how everything works so I've probably messed it up more ways than one.
So the FOR loop has a /R option which will run the same command across all subdirectories.
In your case, I think you want:
for /R %%a in (.) do call :MyCopyFunction %%a
exit /B
:MyCopyFunction [%1 is the current directory]
echo My Current directory is %1
[Do All your code here, using %1 as the current directory]
exit /B
I don't understand what your script is doing in detail, so you still have to fill in the details.
But this shows you how to traverse the entire directory structure.
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")