Batch file to search files in a folder - excel

I need a bat file to search excel files in a folder. It should do the following
search in a folder
looks for excel files after created date
if it is older than system date deletes the excel file
if not it runs a converter.js
Can any one help.

Here you go. Remove the echo's from the script once you see good output on the screen. Also, I'm not sure how you are running converter.js so you'll need to change that line to however you do it and if your file dates are in a different format, you'll have to re-arrange the "%MM%/%DD%/%YYYY%" too.
#echo off
setlocal enabledelayedexpansion
pushd "C:\location\of\files\"
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
for /f "tokens=*" %%a in ('dir /o-d /b /tc *.xls') do (
for /f "tokens=1" %%b in ("%%~ta") do (
if "%%b" NEQ "%MM%/%DD%/%YYYY%" (
echo del "%%a"
) ELSE (
echo run converter.js
)
)
)
popd

Related

Cannot set environmental variable inside a loop in a batch script

I have a batch script that works great and it does the following:
download EDI files from sftp server
generate a dynamic ftp scripts based on the filenames that were downloaded
uploads the file to an internal ftp server via FTP script
I need to modify the batch file to look at the content as some data coming in maybe missing a 0. I do have the logic on how to do this, however, I cannot add the logic to the loop I have as variables do not work.
Below is loop where I want to add the part to correct missing leading zero (that would be in position 4):
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
What I need to do is add an environment variable which will be the content of the file and below is the sample code:
set /p string =< %%A
echo Value for string is set to %string%
echo %string:~9,1% | find "ECHO is on." > nul
if not errorlevel 1 goto Found0
echo no space found
goto End
:Found0
echo found space
echo below is what the correct number needs to be
echo %string:~0,3%0%string:~3% > %%A
:End
I need this to work in batch file as security requirements does not allow PowerShell on the servers.
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
for /F "delims=" %%S in (%%A) do (
setlocal enabledelayedexpansion
set "string=%%S"
if "!string:~9,1!"==" " echo !string:~0,3!0!string:~3!> %%A
endlocal
)
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
should do what you appear to need. Test on sample data first, of course.
Here's the how : Stephan's DELAYEDEXPANSION link
And it saves a lot of work if you tell us what you want to do rather than how you want to do it.

Remove Brackets from Variable (CLI-Windows)

Based on the code below, the output of Var is (COM19). I was wandering what command can be used to remove the brackets? Tried using sed, but would ideally want a solution that doesn't not require additional installations on Windows.
#echo off
setlocal
wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=4" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a
pause
goto :EOF
for /f "delims=()" %%a in ("%output%") do (echo %%a)
Managed to solve the issue ussing the delims, in line with the token function. Code can be seen below.
#echo off
setlocal
wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=2 delims=()" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a
pause

Create Spreadsheet in Excel of Name and File Type Using Script

I have a bunch of excel files and other types of files that I need to populate into a spreadsheet to keep track of them. I was what the batch script would look like for such task. Also I want to be able to pin point which folders I want to get the file names from.
Thanks in advance
Dir does do multiple folders.
dir c:\windows "c:\program files\e*.txt" /a
Here's doing My Docs.
for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"') do set doc=%A
for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"^|findstr /l /v "Pictures Music Video"`) DO #echo %j&echo.

Batch file search & create with more than one word

I need your help, I am fairly new at this type of scripting, and I need your help to try and get this script to function correct.
I have a script that searches a folder for files and moves them to a folder name after the files first characters. But I have an issue when the files are seperated by more than one '.'.
setlocal EnableDelayedExpansion
set "IncomingFolder=D:\Test"
set "showsFolder=D:\Test\"
for %%F in ("%incomingFolder%\*.S*.*") do ( for /f "delims=.S" %%A in ("%%~nF") do (
if not exist "%showsFolder\%" md "%showsFolder%\%%A"
move "%%F" "%showsFolder%\%%A" ) )
For example:
If the files is Hustle.SXX.XXX Then it makes the correct folder named "Hustle".
But if it is named The.Hustle.SXXX.XXX then it makes a folder called "The" only, I need it to use the whole name until ".S".
Anybody who can help me with this?
#ECHO OFF
SETLOCAL
set "IncomingFolder=c:\sourcedir"
set "showsFolder=D:\Test"
for %%F in ("%incomingFolder%\*.S*.*") do (for /f "delims=." %%A in ("%%~nF") do (
ECHO md "%showsFolder%\%%A"
ECHO move "%%F" "%showsFolder%\%%A\"
)
)
GOTO :EOF
I've changed the directory names to suit my system.
I could not recreate the problem you describe, but delims=.S would make the delimiters . or S, not .S. That may be what you are seeing.
The required MD commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories. Append 2>nul to suppress error messages (eg. when the directory already exists)
Also, 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)
You can't use FOR /F to parse from the front because you don't know how many . may appear in the name. I'm assuming you are expecting names like part1.part2.Spart3.part4, but not like part1.part2.Spart3.part4.part5. In other words - you don't want two dots after the .S. If so, then you can safely remove everything after the second to last dot. That is easily done using the ~n modifier twice.
for %%F in ("%incomingFolder%\*.S*.*") do for %%A in ("%%~nF") do (
if not exist "%showsFolder%\%%~nA" md "%showsFolder%\%%~nA"
move "%%F" "%showsFolder%\%%~nA\"
)
(Tickled by Magoo - % missing on "incoming" and ~n required on move-destination)
This uses a helper batch file called repl.bat - download from: https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat
Place repl.bat in the same folder as the batch file or in a folder that is on the path.
Test this to see how it goes:
#echo off
set "IncomingFolder=D:\Test"
set "showsFolder=D:\Test"
for %%F in ("%incomingFolder%\*.S*.*") do (
for /f "delims=" %%A in (' echo "%%~nF"^|repl ".(.*)S\d\d.*" "$1" ') do (
md "%showsFolder%\%%A" 2>nul
move "%%F" "%showsFolder%\%%A"
)
)
setlocal EnableDelayedExpansion
set "IncomingFolder=D:\Test"
set "showsFolder=D:\Test"
for %%F in ("%incomingFolder%\*.S*.*") do (
set "file=%%~nF"
for /F "delims=" %%A in ("!file:*.S=!") do set "folder=!file:.S%%~A=!"
if not exist "%showsFolder%\!folder!" md "%showsFolder%\!folder!"
move "%%F" "%showsFolder%\!folder!"
)
set "file=%%~nF" get the file name, for example: set "file=The.Hustle.SXXX.XXX"
!file:*.S=! is the part after the first ".S", for example XXX.XXX, so
set "folder=!file:.S%%~A=!" is file name without ".SXXX.XXX", for example: "The.Hustle"

Is it possible rename all files in a directory to 0.jpg, 1.jpg, 2.jpg, etc?

I have a bunch of images (100+) in a directory, all with different names. Is there any way to rename them, possibly with a script (I'm running Windows), to 0.jpg, 1.jpg, 2.jpg, etc... without having to rename each one individually? I could launch a Linux virtual machine and copy them over if it isn't possible in Windows.
I've got this so far
#echo off
setlocal enableDelayedExpansion
set MYDIR=F:\Pictures\Wallpapers
set /a count = 0
for /F %%x in ('dir /B/D %MYDIR%') do (
echo %%x
#echo !count!
set /a count+=1
)
Which display the correct file name and the correct counter, but when I try
ren %%x !count!.jpg
Tells me "The system could not find the file specified."
You are not providing the full path for the source file. Don't forget that %%x is just the file name; you need to prepend %MYDIR% to have a complete path:
ren %MYDIR%\%%x !count!.jpg

Resources