DosTips.com

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

All times are UTC-06:00




Post new topic  Reply to topic  [ 33 posts ]  Go to page Previous 1 2 3 Next
Author Message
PostPosted: 24 Jan 2014 00:48 
Offline

Joined: 27 Mar 2013 01:29
Posts: 244
Location: Bozen
using an elevated cmd window, you can make your own short file name:
Code: Select all
fsutil file setshortname "%cd%\test.tmp" blah

This also works for folders.


Top
   
PostPosted: 24 Jan 2014 01:23 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
That's interesting Endoro.

As the short filename bug seems fixed in Vista and later then %%~sfa seems to be the easiest solution - XP users need to upgrade in April anyway. ;)


Top
   
PostPosted: 24 Jan 2014 01:46 
Offline

Joined: 27 Mar 2013 01:29
Posts: 244
Location: Bozen
Btw. %~sI and %~fsI give the same result, but M$ doesn't know it:
Code: Select all
The modifiers can be combined to get compound results:

    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line


Part of the 'for' help text (Win8).


Top
   
PostPosted: 24 Jan 2014 03:11 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
hehe

This works too:

Code: Select all
@echo off
for %%a in (*) do echo "%%~pxnda"


Top
   
PostPosted: 26 Jan 2014 09:36 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
I found a simpler workaround:
Code: Select all
@echo off
cls
setlocal
set "filename=a filename with, a comma [1].txt"
if not exist "%filename%" ((echo test)>"%filename%")
if not exist "%SystemRoot%\system32\command.com" echo this workarround needs command.com.

:: get short form of the absolute file name
pushd .
%SystemRoot%\system32\command.com /? >nul 2>nul
set /A "N=2" & for %%a in ("%filename%") do for %%a in (%%~ta) do set /A "N+=1"
for /F "tokens=%N%" %%a in ('^(dir "%filename%" /X 2^>^&1^)^| findstr /L /C:"%filename%"') do set "sfile=%CD%\%%~a"
popd

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

endlocal
goto :eof

Or in other words: Does the following code works in other Windows versions with "command.com", too (funny way, to get the short path :lol: )?
Code: Select all
@echo off
pushd .
echo actual path: %CD%
%SystemRoot%\system32\command.com /? >nul 2>nul
echo short  path: %CD%
popd


penpen


Top
   
PostPosted: 26 Jan 2014 09:52 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
Yep, it works in Win 8.1

Nice work.

actual path: "d:\abc\abc & def\123 456"
short path: "d:\abc\ABC&DE~1\123456~1"
Press any key to continue . . .


WMIC DATAFILE WHERE name="d:\\abc\\ABC&DE~1\\123456~1\\AFILEN~1.TXT" GET lastmodified
20140127025101.025142+660
Press any key to continue . . .



Top
   
PostPosted: 27 Jan 2014 04:34 
Offline

Joined: 24 Jun 2013 17:10
Posts: 446
Location: Bulgaria
My first impulse here was to try with WQL query with like+wildcards , but I cant do it work for data file :? .While it works fine with services for example...

Code: Select all
c:\>echo test>c:\test.file
c:\>wmic DataFile where 'path like "%C:\\test.file.bat%"' get lastmodified


This always returns `No Instance(s) Available.` Is it even possible to call wmic+like and datafile?


Top
   
PostPosted: 27 Jan 2014 05:43 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
If wmic query on xp does not differ from other windows versions, you have some errors/wrong assumptions in there, see this example:
Code: Select all
Z:\>md test.path

Z:\>echo test>"z:\test.path\test.file"

Z:\>echo test2>"z:\test.path\test2.file"

Z:\>wmic DataFile where 'drive="Z:" and path like "%test.path%"' get lastmodified
LastModified
20140127121614.497867+060
20140127121618.834630+060

Z:\>wmic DataFile where 'drive="Z:" and path like "%test%"' get lastmodified
LastModified
20140127121614.497867+060
20140127121618.834630+060

penpen


Top
   
PostPosted: 27 Jan 2014 08:39 
Offline

Joined: 24 Jun 2013 17:10
Posts: 446
Location: Bulgaria
ahaam. the path property does not contain the drive.But still cannot make it work in all cases at least on my machine.


Quote:
c:\tmpf>cd c:\

c:\>md tmpf2

c:\>cd tmpf2

c:\tmpf2>echo a>testFileWithUniqueName

c:\tmpf2>wmic DataFile where 'drive="C:" and name like "%testFileWithUniqueName%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and path like "%testFileWithUniqueName%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and name like "%tmpf2%"' get lastmodified,path,name
No Instance(s) Available.

c:\tmpf2>wmic DataFile where 'drive="C:" and path like "%tmpf2%"' get lastmodified,path,name
No Instance(s) Available.


But have no problem to find older files...Also I have a suspicious that if there's an variable like %expressions% it is substituted


Top
   
PostPosted: 27 Jan 2014 11:50 
Offline
Expert

Joined: 23 Jun 2013 06:15
Posts: 1441
Location: Germany
This is strange... the second wmic command line produces "No Instance(s) Available." as the filename is not its path,
but the other should work, so the output should look like this:
Code: Select all
Z:\>test

Z:\>md tmpf2

Z:\>cd tmpf2

Z:\tmpf2>echo a 1>testFileWithUniqueName

Z:\tmpf2>echo b 1>b

Z:\tmpf2>wmic DataFile where 'drive="Z:" and name like "%testFileWithUniqueName%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\


Z:\tmpf2>wmic DataFile where 'drive="Z:" and path like "%testFileWithUniqueName%"' get lastmodified,path,name
Keine Instanzen verfügbar.



Z:\tmpf2>wmic DataFile where 'drive="Z:" and name like "%tmpf2%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.153542+060  z:\tmpf2\b                       \tmpf2\
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\


Z:\tmpf2>wmic DataFile where 'drive="Z:" and path like "%tmpf2%"' get lastmodified,path,name
LastModified               Name                             Path
20140127183824.153542+060  z:\tmpf2\b                       \tmpf2\
20140127183824.143528+060  z:\tmpf2\testfilewithuniquename  \tmpf2\
Maybe the xp wmic differs from your oses one.

penpen

Edit: I get your output if the paramters interferes with environment variables only, for example (actually i have no other idea what might happened):
Code: Select all
set "testFileWithUniqueName=abc"
set "tmpf2=def"


Top
   
PostPosted: 28 Jan 2014 16:26 
Offline

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

I wondered why nobody replied to my posts. Then I realised Foxi was talking about square brackets in the filename and not the parentheses around the name clause of the wmic command. :oops:

Anyways, here’s my mshta solution which is very much in vogue these days thanks to npocmaka and Einstein1969:

Code: Select all
set "prop=LastModified"
set lm1=CreateObject(""Scripting.FileSystemObject""^).^
GetStandardStream(1^).Write GetObject(""winMgmts:CIM_DataFile.Name
set lm2=""^).%prop%:Close

for /f %%m in ('mshta vbscript:execute("%lm1%='%~dpf1'%lm2%"^)') ^
do echo(file "%~nx1" last modified on: %%m


Works with poison characters in folders and filenames. Change LastModified to CreationDate or any other CIM_DataFile property name you want. Unlike wmic, there’s no CR at end of string. And thanks to Dave Benham for pointing out that there’s no need to pipe mshta’s output through find or more if mshta is inside a for /f loop’s in (…) clause.

- SB

Edit: Tidied up code snippet. Added link to CIM_DataFile.


Last edited by Sponge Belly on 03 Feb 2014 10:32, edited 1 time in total.

Top
   
PostPosted: 29 Jan 2014 03:05 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 6033
Sponge Belly wrote:
I realised Foxi was talking about square brackets in the filename and not the parentheses around the name clause of the wmic command. :oops:


It was commas originally, and the work around to fix them broke round brackets.

Your solution works here in Windows 8.1


Top
   
PostPosted: 29 Jan 2014 16:49 
Offline

Joined: 01 Oct 2012 13:32
Posts: 181
Location: Ireland
Thanks Foxi! :D

I forgot to mention that it should also work for any poor Windows XP Home bunnies out there who don't have the wmic command available to them.

Btw, does anyone know how to do the mshta equivalent of the following:

Code: Select all
wmic os get localdatetime


It might prove useful.

TIA! ;-)

- SB


Top
   
PostPosted: 29 Jan 2014 17:35 
Offline

Joined: 24 Jun 2013 17:10
Posts: 446
Location: Bulgaria
Sponge Belly wrote:

Btw, does anyone know how to do the mshta equivalent of the following:
Code: Select all
wmic os get localdatetime


It might prove useful.

TIA! ;-)

- SB


Here it is http://msdn.microsoft.com/en-us/library ... 71(v=vs.85).aspx

Win32_LocalTime class --> http://www.activexperts.com/admin/scrip ... ript/0130/


Top
   
PostPosted: 31 Jan 2014 15:24 
Offline

Joined: 01 Oct 2012 13:32
Posts: 181
Location: Ireland
Thanks for the pointers, npocmaka. :-)

But this thread on a Russian forum proved to be instructive. Retouching the script I found there, I ended up with:

Code: Select all
set ^"js=javascript:close(new ActiveXObject('Scripting.^
FileSystemObject'^).GetStandardStream(1^).Write(new String(new ^
Enumerator(new ActiveXObject('WbemScripting.SWbemLocator'^).^
ConnectServer('.','root\\cimv2'^).ExecQuery('Select LocalDateTime ^
from Win32_OperatingSystem where Primary=True'^)^).item(^).^
LocalDateTime^)^)^);^"

for /f %%t in ('mshta "%js%"') do echo(WMI LocalDateTime: %%t


Long-winded, but it works! And it should work for everyone, even any poor Windows XP Home bunnies still left out there. ;-)

- SB

PS: Rewrote LocalDateTime code in VBScript and it turned out to be less verbose than the JavaScript version! :shock:


Last edited by Sponge Belly on 03 Feb 2014 10:05, edited 2 times in total.

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

All times are UTC-06:00


Who is online

Users browsing this forum: Google [Bot] and 13 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