I believe a windows update broke my .bat file.
What this .bat file is supposed to do is update files within the folder with a date and time using nircmd.exe.
All file names start with mmddyy for reference.
Here is the .bat file code.
REM #echo on
REM setlocal enabledelayedexpansion
FOR /R C:\Users\USERNAME\Desktop\optimize %%F in (*.*) DO call :Setfiletime %%F
goto End
:Setfiletime
SET FNAME=%~n1
echo %FNAME%
SET MM=%FNAME:~0,2%
SET DD=%FNAME:~2,2%
SET YY=%FNAME:~4,2%
nircmd.exe setfiletime %1 "%DD%-%MM%-20%YY% 18:00:00" "%DD%-%MM%-20%YY%18:00:00"
:goto :eof
:End
REM endlocal
The output now shows each line before :Setfiletime, then ends with REM.
Why is this batch file no longer working and what would I need to do to fix it?
EDIT: Fixed - file was in the wrong location. The simplest solution was the best answer.
Related
I need a batch file that renames my files in according to the folder name.
For example I have this folder:
E:\PROGET\01_Progetti\1_TRATTATIVE\IT.16.9291_Fabbricato ad Milano (MI) - Ing. Bianchi\03-CALCOLO\02-XLS\
Which contains
CB-Tech_92XX - .xls
Punz_92XX - .xls
I want to rename them to
CB-Tech_9291 - .xls
Punz_9291 - .xls
Is it possible?
EDIT:
I've found this code from a guy who asked for code and didn't get any complain Rename all files in a directory with a Windows batch script
I've changed it a little bit:
#echo off
setlocal enableDelayedExpansion
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=9XXX!"
)
#pause
Now I just have to understand how to get the path (done), extract only the numbers (not yet) and store in a variable (done).
To set a variable should be something like that
set "number=projectnumber"
SET mypath=%~dp0
ok now I've the path, need to extract only 4 characters after the IT.16.
Will edit later :)
EDIT 2:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set projectnumber=%mypath:~41,4%
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
#pause
YEAH! This works for this specific folder!!
Now I just need to understand how to search and extract the number inside the path to make it more general.
I'm looking for a function that returns the position of the first char of the string IT.16.
Any advice?
This is the complete solution of this question:
#echo off
setlocal enableDelayedExpansion
SET mypath=%~dp0
set "projectnumber=%mypath:*IT.16.=%"
set "projectnumber=%projectnumber:~,4%"
for %%F in (*92XX*) do (
set "name=%%F"
ren "!name!" "!name:92XX=%projectnumber:~0%!"
)
I have a question. Is it possibile in batch language to search in folder a part of name that is same like another file and display it.For example i got folder with files :
ggggggsss.mp3
ddddddeee.mp3
ddddddff.mp3
ssssssddd.mp3
aaaaasssss.mp3
11111ssdas.mp3
11111dddd.mp3
...
I need to display in cmd only names of files
ddddddeee
ddddddff
and
11111ssdas
11111dddddd
Because the first six letter are the same. Could someone help me with this problem?
Save this script to test.bat and run from open Cmd Prompt. Replace dir value with path to your folder with .mp3 files:
#echo off
setlocal enabledelayedexpansion
set "dir=%userprofile%\music"
set "pattern1=dddddd" & set "pattern2=11111"
pushd "%dir%"
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,6!"=="%pattern1%" echo %%G)
echo/
FOR %%G IN (*.mp3) DO ( set song=%%G
if "!song:~0,5!"=="%pattern2%" echo %%G)
popd
exit /b
See also Extract Substrings.
I have a script that I can run in the command line to get me the version of software. It works perfectly in the command line. I type this in getversion "<full path>" and it gets me exactly what I need.
Now the catch is that I have to have the getversion.bat and a vbscript file both in the directory that I'm in for the command line. This is probably a dumb question but if I want to add this into a batch script where the version is set as a variable how would I do that?
right now I had it looking like this
#echo off
set version=getversion "<full path>"
echo %version%
pause>nul
The problem seems to be that the batch file doesn't know where to find getversion.bat or the vbscript referenced in that script. How can I tell the batch file where they are?
One way to accomplish this is to direct the output of your function to a file and then read this output back into a local variable using a SET /P command. This should do it:
SET TempFile="%Temp%\%RANDOM%.txt"
REM Direct output to a temp file.
CALL getversion "<full path>">%TempFile%
REM Read the output written to the temp file into a local variable.
SET /P Version=<%TempFile%
ECHO %Version%
REM Cleanup.
IF EXIST %TempFile% DEL %TempFile%
Alternately (and this way much "cleaner"), you can use a FOR command to run your function and store the output into a local variable:
FOR /F "usebackq tokens=* delims=" %%A IN (`CALL getversion "<full path>"`) DO SET Version=%%A
ECHO %Version%
I'm trying to create a batch file that would rename a bunch of files in a folder. These files would have a naming of something like: blah(lol).txt. There will always be a four letters, followed by an open bracket, three letters, and finally a close bracket.
I want the batch file to remove the bracketed part of the name of the file, ie. rename the file without the bracketed part.
for %%i IN (*.txt) DO (set name=%%~ni
set name2=%name:~1,4%
ren %%i %name2%)
Why doesn't this work?
Magoo provided an explanation as to why your script failed, as well as a working script.
But in your case, there is no need for a script. A simple REN command is all that is needed:
ren "????(???).txt" "????.*"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=1,2,3delims=()" %%a IN (
'dir /b /a-d "%sourcedir%\*(*).*" '
) DO ECHO REN "%sourcedir%\%%a(%%b)%%c" %%a%%b%%c
GOTO :EOF
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO REN to REN to actually rename the files.
Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).
Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion and use !var! in place of %var% to access the changed value of var or 2) to call a subroutine to perform further processing using the changed values.
simple but works from the folder with the files to be renamed.
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo File Renamed
ping -n 3 127.0.0.1 >NUL
goto begin
a much simpler approach ... try a for loop that cycles through all files in your folder
I'm going to use lol as an example of three letter word inside brackets as stated in your question
#echo off
for %%a in (*) do (
rename "%%a" "%%a(lol).exe"
)
to use this batch file you have to place it in the folder containing the files you wanna rename
I'm fairly new to batch scripting, and I need to write a pretty simple .bat file that will loop through a directory. I know how to do it pretty easily using the goto command:
#echo off
:BEGIN
::set variable to data in first file
::do operations on file...
IF ::another file exists in the directory
::increment to next file
GOTO BEGIN
ELSE
GOTO END
:END
cls
The problem is that's the only way I can think of to do it. I know goto's are generally very frowned upon to use, so I was wondering if anyone knows another way to do this? Thanks!
Replace the echo.... with your desired command.
From the Command prompt:
for /R %A in (*.*) do echo.%A
In a bat file
for /R %%A in (*.*) do echo.%%A
It can be done just for the current folder too. The %%a metavariable is case sensitive and I choose to use lower case. The script will exit and quit when all files are processed.
#echo off
for %%a in (*.txt) do (
type "%%a"
pause
)