2

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"
anub13
  • 69
  • 1
  • 9
  • Which double quotes? I see three sets. – SomethingDark Sep 13 '17 at 05:57
  • @anub13, please delete your comments and edit your question with any additional information you have. – Squashman Sep 13 '17 at 06:38
  • The `DO` has to be on the same line as the `FOR`. I think what you are trying to do is this. `for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv') do >>C:\temp\count.txt echo %%~ni` – Squashman Sep 13 '17 at 06:48
  • Hi Squashman, thanks I will try later, at the moment occupied. Be right back soon. – anub13 Sep 13 '17 at 06:58
  • just did @Squashman Thanks, now will try your suggestion. – anub13 Sep 13 '17 at 07:27
  • Just try and works! but.. is there a way to get rid of %%~ni showing in body mail? EDIT: I dont know why previously %%~ni was showing, edit here and there and its gone EDIT 2: when running second time, its not overwritting previous .txt file. I think I need to delete if exist.. – anub13 Sep 13 '17 at 08:36

2 Answers2

1

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
::********************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Shouldn't need to set the FOR variable to an environmental variable before redirecting the output. Also shouldn't need `/c "cmd /c echo @fname"` because you can get the base file name without the extension with the `FOR` variable modifier. So the base code could really just look like this: `for /f "delims=" %%i in ('FORFILES /D %now% /m *.csv') do >>C:\temp\count.txt echo %%~ni` – Squashman Sep 13 '17 at 06:51
  • Hi Hackoo, Thanks for responding, just now running your suggestion, it doesn't give output at all. Instead, its opening new .txt file and wrote !MyVariable! – anub13 Sep 13 '17 at 06:55
  • @Hackoo OK, will do. – anub13 Sep 13 '17 at 07:28
  • @Hackoo just now tried, and the output is exactly the same as before, I assume, its confuse where is the source file is? Because I don't see the source syntax/variable. – anub13 Sep 13 '17 at 07:30
  • @anub13 what is the path of the folder that contains `*.csv` ? – Hackoo Sep 13 '17 at 07:38
  • C:\Users\user1\Documents\Work\warehouse – anub13 Sep 13 '17 at 07:42
  • @anub13 re-check my last edit based on your given path – Hackoo Sep 13 '17 at 07:48
  • @Hackoo it works! Thanks, very appreciate it.. but I prefer my own code because its easier to understand. Once again, thanks very appreciated your effort. – anub13 Sep 13 '17 at 07:53
  • @Hackoo thanks, just check again your answer it has syntax to delete previous existing file.. I will update it to my code. (y) was trying second time with dummy file, its duplicating instead overwriting previous .txt file – anub13 Sep 13 '17 at 08:38
0

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
anub13
  • 69
  • 1
  • 9