DosTips.com

A Forum all about DOS Batch
It is currently 22 Nov 2017 12:40

All times are UTC-06:00




Post new topic  Reply to topic  [ 33 posts ]  Go to page 1 2 3 Next
Author Message
PostPosted: 21 Jan 2014 08:16 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
Has anyone solved the problem of using a comma in a filename and processing it with WMIC?

I get an error ", - Invalid alias verb."

Code: Select all
@echo off
set "file=a filename with, a comma.txt"
echo text>"%file%"
WMIC DATAFILE WHERE name="%file%" get lastmodified | find "."
pause


Top
   
PostPosted: 21 Jan 2014 10:58 
Offline

Joined: 17 Nov 2013 15:00
Posts: 45
Location: Germany
I may have a solution:
Quote:
WMIC DATAFILE WHERE (name="%file%") get lastmodified | find "."

however, if i run your script with the parenthesis i get another error:
Quote:
Keine Instanzen verfügbar.

it should translate to something like "No instances available".


Top
   
PostPosted: 21 Jan 2014 11:53 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
In WinXP prof it is no matter of a comma, but a matter of full path with escaped '\', so try:
Code: Select all
@echo off
set "file=%~dp0a filename with, a comma.txt"
echo text>"%file%"
WMIC DATAFILE WHERE name="%file:\=\\%" get lastmodified | find "."
pause

penpen

Edit: My xp was not at actual patch level: Now this won't work anymore.


Last edited by penpen on 22 Jan 2014 18:21, edited 1 time in total.

Top
   
PostPosted: 21 Jan 2014 14:01 
Offline

Joined: 01 Oct 2012 13:32
Posts: 181
Location: Ireland
Hello All! :-)

I combined AiroNG’s parentheses and Penpen’s backslash-escape techniques and ended up with something like this:

Code: Select all
X:\> set "fn=%cd%\tom, dick & harry.txt"
X:\> type nul >"%fn%"
X:\> wmic datafile where (name="%fn:\=\\%") get lastmodified
LastModified
20140121191739.539644+000


I haven’t tested this against all poison characters, but it looks promising.

- SB


Top
   
PostPosted: 21 Jan 2014 15:11 
Offline
Expert

Joined: 16 May 2011 08:21
Posts: 1441
Location: Flanders(Belgium)
AiroNG wrote:
I may have a solution:
Quote:
WMIC DATAFILE WHERE (name="%file%") get lastmodified | find "."

however, if i run your script with the parenthesis i get another error:
Quote:
Keine Instanzen verfügbar.

it should translate to something like "No instances available".
does the file exist ?
Code: Select all
@echo off &setlocal disableDelayedExpansion
ver
set "f=c:\\___.txt"
echo.>"%f%"
WMIC DATAFILE WHERE ( name='%f%' AND Archive='TRUE' ) get lastmodified | find "."
set "f=c:\\_,_.txt"
echo.>"%f%"
WMIC DATAFILE WHERE ( name='%f%' AND Archive='TRUE' ) get lastmodified | find "."

pause
it works for me..
Code: Select all
Microsoft Windows XP [versie 5.1.2600]
20140121213757.484375+060
20140121213757.484375+060
Druk op een toets om door te gaan. . .
if it were standard SQL it would work always :roll:


Top
   
PostPosted: 21 Jan 2014 16:31 
Offline

Joined: 27 Mar 2013 01:29
Posts: 244
Location: Bozen
you may have an error:
Code: Select all
C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('WMIC DATAFILE WHERE ( name^='%f%' AND Archive^='TRUE' ^) get lastmodified ^| find "."') do @echo %a
No Instance(s) Available.



you may have a comma:
Code: Select all
C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('"WMIC DATAFILE WHERE name='%f%' get lastmodified | find ".""') do @echo %a
, - Invalid alias verb.



or not: :roll:
Code: Select all
C:\Users\User\TEST>set "f=c:\\_,_.txt"

C:\Users\User\TEST>for /f %a in ('^"WMIC DATAFILE WHERE name='%f%' get lastmodified ^| find "."^"') do @echo %a
c:\\_ _.txt - Invalid alias verb.


Who knows the quoting for the loop?


Top
   
PostPosted: 21 Jan 2014 17:23 
Offline

Joined: 01 Oct 2012 13:32
Posts: 181
Location: Ireland
Hi Endoro! :-)

Great question. Turns out the equals sign after name must be caret-escaped:

Code: Select all
X:\> set "fn=%cd%\tom, dick & harry.txt"
X:\> type nul >"%fn%"
X:\> for /f "skip=1" %f in ('
More? wmic datafile where (name^="%fn:\=\\%"^) get lastmodified
More? ') do @echo(%f
20140121224248.567011+000


It took me quite a while to track this one down. It’s always the little things that trip you up. ;-)

- SB


Last edited by Sponge Belly on 22 Jan 2014 06:29, edited 1 time in total.

Top
   
PostPosted: 21 Jan 2014 17:33 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 2990
Location: Germany
The key is to use double quotes instead of single quotes around the file path.

Regards
aGerman


Top
   
PostPosted: 21 Jan 2014 22:34 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
I did google before posting and what I found was that when using parentheses, it fixes the comma, but then the closing bracket becomes a poison character in a filename.

I didn't find any solution that fixes the comma without introducing the closing bracket problem. I thought maybe there was some technique that google wasn't showing me.


Top
   
PostPosted: 22 Jan 2014 18:26 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
The 8.3 filename format seems to disallows the use of a comma:
Code: Select all
 Datenträger in Laufwerk Z: ist Test
 Volumeseriennummer: 0123-4567

 Verzeichnis von Z:\

23.01.2014  01:21                 0 A_B~1.TXT    a, b.txt
23.01.2014  01:21                 0 A_B~2.TXT    a,b.txt
               2 Datei(en)              0 Bytes
               0 Verzeichnis(se), 162.422.374.400 Bytes frei
So if short names are available this script may do what you want:
Code: Select all
@echo off
setlocal
set "filename=a filename with, a comma.txt"
set "file=%~dp0%filename%"

for /F "tokens=1-3*" %%a in ('dir /X "%filename%" ^| findstr /R /C:"%filename%"') do set "shortname=%%d"
call set "shortname=%%shortname:%filename%=%%|"
for /L %%a in (1,1,11) do set "shortname=%shortname: |=|%"
set "shortname=%shortname:|=%"

set "sfile=%~dp0%shortname%"
WMIC DATAFILE WHERE name="!sfile:\=\\!" GET lastmodified | find "."

pause
endlocal

penpen


Top
   
PostPosted: 22 Jan 2014 23:11 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
Your idea works penpen (the sfile needs to be using %var% syntax), though it will fail where the time indicator has am/pm, without some extra logic - true?

I wonder if the following will work in previous windows versions also, or if the DIR layout is different to Win 8.x?

The thought occurs to me that the folder path could contain a comma too, and getting the short path and filename reliably is a real chore.

Code: Select all
@echo off
setlocal
set "filename=a filename with, a comma.txt"
echo abc>"%filename%"

for /F "delims=" %%a in ('dir /X "%filename%" ^| findstr /I /R /C:"%filename%"') do set "line=%%a"
set "shortname=%line:~36,12%"
set "shortname=%shortname: =%"
if defined shortname (set "filename=%~dp0%shortname%") else (set "filename=%~dp0%filename%")

WMIC DATAFILE WHERE name="%filename:\=\\%" GET lastmodified | find "."

pause
endlocal


Top
   
PostPosted: 23 Jan 2014 03:59 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
This "=%line:~36,12%" part seems to on all systems with default short date format (TT.MM.JJJJ), but if other date formats are in use (for example "T.M.JJ" or "TTTT, TT.MM.JJJJ") it may slides to the left or right.

Above i didn't know how the 8.3 format is used, and if spaces and commas are really disallowed, but luckily i've found:
https://support.microsoft.com/kb/142982/EN-US

Also i have reread the for help, and found something very usefull :D .
So this shortest, that should (hopefully) work everywhere is:
Code: Select all
@echo off
setlocal
set "filename=a filename with, a comma.txt"

if not exist "%filename%" echo test>"%filename%"

for %%a in ("%filename%") do set "sfile=%%~sdpnxa"
WMIC DATAFILE WHERE name="%sfile:\=\\%" GET lastmodified | find "."

pause
endlocal

penpen


Top
   
PostPosted: 23 Jan 2014 07:34 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
penpen wrote:
This "=%line:~36,12%" part seems to on all systems with default short date format (TT.MM.JJJJ), but if other date formats are in use (for example "T.M.JJ" or "TTTT, TT.MM.JJJJ") it may slides to the left or right.


Thanks, I guess that is another damper on my method.

Quote:
for %%a in ("%filename%") do set "sfile=%%~sdpnxa"


This would be perfect, but for many many years the short path\filename feature of %%~s has a bug where in certain cases the path\filename is corrupted as a segment of the string is added to the end.

EDIT: see this thread for some further discussion, but related to short filename bug using %%~s viewtopic.php?f=3&t=5310


Top
   
PostPosted: 23 Jan 2014 07:36 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
I've found the bug: http://stackoverflow.com/questions/8354305/batch-parameter-s1-gives-incorrect-8-3-short-name

I've finished a workaround, not short, but it seems to be reliable now:
Code: Select all
@echo off
cls
setlocal
set "filename=a filename with, a comma [1].txt"
if not exist "%filename%" echo test>"%filename%"


for %%a in ("%filename%\") do set "buffer=%%~dpnxa"
set "N=-1"
for %%a in ("%buffer:\=" "%") do set /A "N+=1"
for %%a in ("%filename%\") do set "buffer=%%~sdpa"
set "M=0"
set "sfile="
setlocal enabledelayedExpansion
for %%a in ("%buffer:\=" "%") do (
   set /A "M+=1"
   if !M! LEQ !N! set sfile=!sfile! "%%~a"
)
endlocal & set sfile=%sfile%
set sfile=%sfile:" "=\%
set "sfile=%sfile:"=%"
set "sfile=%sfile: =%"
echo %sfile%


echo WMIC DATAFILE WHERE name="%sfile:\=\\%" GET lastmodified | find "."

pause
endlocal

penpen


Top
   
PostPosted: 23 Jan 2014 08:55 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
I can confirm that this worked in XP Pro in a VM.

I also tried the test case I listed in viewtopic.php?f=3&t=5310 using a Vista VM and the short filename bug looks like it was fixed in Vista.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 33 posts ]  Go to page 1 2 3 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 15 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited