Behavior of "?" is fishy when trying to rename all the files of current folder(thro' Batch Script) expecting that extension would be unchanged - windows

I have been scripting in Windows(7 64Bit) Batch engine for sometime now.
What I find really disappointing is the rename command when trying to rename all files in a folder and using "?" to keep the file extension unaltered.
Please look at the below set of commands:
#echo off
set /p filename="Desired File Name: "
rename *.* "%filename%.???"
This is supposed to keep the file extension as it is but, on the contrary, if I pass the filename as "MONEY 2016" it unexpectedly renames the files to "MONEY 2016.201", why is that ?
Also, as debugging step, I printed the filename right after getting it inputted from the user, it prints as it is with spaces, so it doesn't seem like space issue at all..
How to make this work as expected with a one-liner as short as possible ?

Here's my comment as an answer.
I'd suggest a For loop as a simple method of performing this task:
#Set /P "filename=Desired File Name: "
#For %%A In (*.*)Do #If Not Exist "%filename%%%~xA" Ren "%%A" "%filename%%%~xA" 2>Nul
Please note that this method does not perform any verification of the user input. They could enter nothing, or they may enter a filename which is invalid. I'll leave it to you to implement a verification process, should you feel it necessary.

You are changing the structure of original and new names. * certainly does not match ??? (this should be obvious). A rename command with the proper structure works well:
C:\Users\Antonio\Tests> dir
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 0895-160E
Directorio de C:\Users\Antonio\Tests
28/03/2019 10:12 p. m. <DIR> .
28/03/2019 10:12 p. m. <DIR> ..
28/03/2019 10:11 p. m. 7 four.srt
28/03/2019 10:11 p. m. 7 one.mkv
28/03/2019 10:11 p. m. 7 three.avi
28/03/2019 10:11 p. m. 7 two.mp4
4 archivos 28 bytes
2 dirs 391,521,251,328 bytes libres
C:\Users\Antonio\Tests> rename *.* NewName.*
C:\Users\Antonio\Tests> dir
El volumen de la unidad C no tiene etiqueta.
El número de serie del volumen es: 0895-160E
Directorio de C:\Users\Antonio\Tests
28/03/2019 10:12 p. m. <DIR> .
28/03/2019 10:12 p. m. <DIR> ..
28/03/2019 10:11 p. m. 7 NewName.avi
28/03/2019 10:11 p. m. 7 NewName.mkv
28/03/2019 10:11 p. m. 7 NewName.mp4
28/03/2019 10:11 p. m. 7 NewName.srt
4 archivos 28 bytes
2 dirs 391,521,251,328 bytes libres

Related

cmd command seporator broken in Windows 10

This is unrelated to Java, however the error occurs when I try to compile my code.
The semicolon in the command is causing Windows cmd to fail
Example
c:\>cd c:\windows <- no error
c:\>cd c:\windows; <- the semicolon is not being used a command separator but as part of the path
The system cannot find the path specified.
cmd doesn't use semicolon as a command separator. If you want to have two commands on the same line, you need to use & or | (depending on what you're trying to do).
; is used as an argument separator on Windows, but it's not supported by most applications. It has nothing to do with cmd, and cmd isn't supposed to deal with it (applications parse their command lines, not cmd). It also doesn't apply to cmd intrinsics like cd (cd is not an application).
If you have a directory with a semicolon in the name try wrapping it in "double quotes."
Can't exactly verify your problem, but see below.
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:53 <DIR> .
11/27/2019 08:53 <DIR> ..
0 File(s) 0 bytes
2 Dir(s) 871243935744 bytes free
Z:\[]>md zz
Z:\[]>md zz;
A subdirectory or file zz already exists.
Z:\[]>md "zz;"
Z:\[]>dir
Volume in drive Z has no label.
Volume Serial Number is CAC7-078B
Directory of Z:\
11/27/2019 08:54 <DIR> .
11/27/2019 08:54 <DIR> ..
11/27/2019 08:54 <DIR> zz
11/27/2019 08:54 <DIR> zz;
0 File(s) 0 bytes
4 Dir(s) 871243935744 bytes free
Z:\[]>cd zz;
Z:\zz;[]>cd ..\zz
Z:\zz[]>cd "..\zz;"
Z:\zz;[]>
The issue has been resolved with the following
Adding -J-Djdk.lang.Process.allowAmbiguousCommands=true to netbeans_default_options= in etc/netbeans.conf
The output from the compile command has not changed, however it now finds the directories???

creating mercurial repo in directory whose name has dollar signs

I am trying to create a mercurial repository in a directory whose name includes dollar signs. This is an equivalent and simplified example of what i get on windows 10 cmd.exe with mercurial 4.1.3 :
C:\test\dir1>hg init
C:\test\dir1>hg status
C:\test\dir1>cd ../dir$$1
C:\test\dir$$1>hg init
C:\test\dir$$1>hg status
abort: repository C:\test\dir$$1 not found!
so i hope this is clear, the only difference seems to be the dollar signs in the second directory name. thanks in advance!
Mercurial seems to treat dollar signs as an environment variable escape:
C:\test>set X=abc
C:\test>echo $X # Not the shell expanding it, that would be %X%.
$X
C:\test>hg init $X
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is CE8B-D448
Directory of C:\test
11/10/2017 09:27 PM <DIR> .
11/10/2017 09:27 PM <DIR> ..
11/10/2017 09:27 PM <DIR> abc
0 File(s) 0 bytes
3 Dir(s) 53,899,231,232 bytes free
Mercurial has expanded $X as an environment variable. Also:
C:\test>hg init dir$$x
C:\test>dir
Volume in drive C has no label.
Volume Serial Number is CE8B-D448
Directory of C:\test
11/10/2017 09:30 PM <DIR> .
11/10/2017 09:30 PM <DIR> ..
11/10/2017 09:30 PM <DIR> dir$x
0 File(s) 0 bytes
3 Dir(s) 53,899,091,968 bytes free
Two dollar signs insert one dollar sign. When you are in a directory named dir$$x, Mercurial is using dir$x for the name. I found a workaround with hg -R. status, but better to avoid dollar signs.
this has been entered as a Mercurial bug : https://bz.mercurial-scm.org/show_bug.cgi?id=5739

Code to determine target of remote junction

Windows 7/NTFS and later has both symbolic links and junctions, and they are subtly different. (See this excellent set of posts). For logging (and debugging) purposes, I need to be able to resolve the target of a junction on a remote file server. There have been some posts on this topic, but they apply to resolving a junction as the local machine see it.
On \\FileServer, we have a directory G:\Shared that is shared out as PublicShare, so it apears on the network as \\FileServer\PublicShare. Within that directory are sub-directories G:\Shared\SubDir1 and G:\Shared\SubDir2. Also within that directory is a junction G:\Shared\Junc that points to either G:\Shared\SubDir1 or G:\Shared\SubDir2. (The target can change.) Thus, on \\FileServer, one sees
\\FileServer\PublicShare\SubDir1
\\FileServer\PublicShare\SubDir2
\\FileServer\PublicShare\Junc
On a client machine where \\FileServer\PublicShare is mounted (mapped in Windows lingo) as M:\, one thus sees M:\SubDir1, M:\SubDir2, M:\Junc. If, on that client machine, you open a console (cmd.exe) and do dir M:\, Windows gives a nice listing which shows that M:\Junc is a junction and includes the target, G:\Shared\SubDirX, of the junction.
M:\>dir
Volume in drive M is XXXXXXXXX
Volume Serial Number is XXXX-XXXX
Directory of M:\
09/05/2014 07:30 PM <DIR> .
09/05/2014 07:30 PM <DIR> ..
09/05/2014 01:36 PM <JUNCTION> Junc [G:\Shared\SubDir1]
09/06/2014 12:55 PM <DIR> SubDir1
09/05/2014 05:15 PM <DIR> SubDir2
0 File(s) 0 bytes
3 Dir(s) 1,895,493,492,736 bytes free
M:\>
Thus, the target of such a junction is clearly available to a client. Does anyone know how to obtain this information programatically, e.g. which system api to call??? Thanks.
This does the trick:
fsutil reparsepoint query "M:\Junc"
and it seems very reliable.
If you don't want to parse the output of C:\Windows\System32\fsutil.exe, you could simply use DIR:
for /f "usebackq delims=[] tokens=2" %i in ( `dir M:\^|findstr JUNCTION` ) do set RES=%i
Notes:
M:\: you need to display the subfolders (and junctions) of the parent folder of the JUNCTION (in this case: M:\)
findstr: if you have several junctions and want to resolve a specific one, add a |findstr foldername to isolate the right line,
delims=[]: that will divide the output 09/05/2014 01:36 PM <JUNCTION> Junc [G:\Shared\SubDir1] into 3 tokens:
09/05/2014 01:36 PM <JUNCTION> Junc
G:\Shared\SubDir1
"<empty>"
tokens=2: only the second token ("G:\Shared\SubDir1") is of interest
%i: if you run that command in a .bat script, double the '%': %%i in and res=%%i

Why does the cmd.exe shell on Windows fail with paths using a forward-slash ('/'') path separator?

Just when I'd thought I'd seen it all with Windows path issues, I've now encountered a case that only fails when '/' (forward-slash) is used as the path separator is used:
C:\temp\tcbugs>mkdir "dir1 with spaces"
C:\temp\tcbugs>echo hi > "dir1 with spaces"\foo.txt
C:\temp\tcbugs>type "dir1 with spaces\foo.txt"
hi
C:\temp\tcbugs>type "dir1 with spaces/foo.txt"
The system cannot find the file specified.
What is particularly interesting about this is that it appears to be specific to the cmd.exe shell and doesn't occur in PowerShell (nor presumably in the win32 API):
PS C:\temp\tcbugs> type 'dir1 with spaces/foo.txt'
hi
Another point of interest is that changing directories with 'cd' and using '/' used as a path separator with cmd.exe does work:
C:\temp\tcbugs>mkdir dir2_no_spaces
C:\temp\tcbugs>cd ./dir2_no_spaces
C:\temp\tcbugs\dir2_no_spaces>cd ..
Yet, I can't find any reference to this particular problem anywhere online nor in MSDN's commonly cited documentation:
Naming Files, Paths, Namespaces
Which leads me to ask: why does this happen, and is there a definitive source which documents this quirk?
UPDATE:
dbenham points out that the problem is present regardless of whether spaces are in a directory name, so removed reference to it in the title and question body. Also added an example of 'cd ./' that works, while other commands don't.
Edited to remove opinion
Whether or not Windows CMD.EXE is supposed to support forward slashes in paths, the fact is sometimes it works, sometimes it doesn't, and sometimes it appears to work but gives the wrong result - AKA a bug.
It's time for some experiments :-)
All tests were run on Vista
C:\>md "c:/temp/"
C:\>REM The forward slash works with MD!
C:\>echo hello world 1>>"c:/temp/test.txt"
C:\>REM Redirection works with forward slashes!
C:\>type "c:\temp\test.txt"
hello world
C:\>REM Of course TYPE works with back slashes
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM But forward slash version fails
C:\>type "c:/temp\test.txt"
hello world
C:\>REM But TYPE works with forward slash as long as last slash is back slash
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM Note how DIR lists the directory with a \, yet fails to find any files
C:\>dir "c:/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
File Not Found
C:\>REM DIR Still fails with forward slashes
C:\>dir "c:/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM But forward slash works if no file is specified!
C:\>dir "c:/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM And DIR works with forward slash as long as last slash is back slash
C:\>REM Now add another folder to the path hierarchy
C:\>md "c:/temp/temp/"
C:\>REM Still can create folder using forward slashes
C:\>copy "c:/temp/test.txt" "c:/temp/temp/"
The system cannot find the file specified.
0 file(s) copied.
C:\>REM Failed to copy with forward slashes
C:\>copy "c:/temp\test.txt" "c:/temp/temp/"
1 file(s) copied.
C:\>REM But forward slash works if last slash before file name is back slash
C:\>REM Rerun some past tests
C:\>type "c:/temp/test.txt"
The system cannot find the file specified.
C:\>REM Good - it still fails
C:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM What is going on?! :( Why did that seem to work now?
C:\>REM More on that later.
C:\>REM Now test the new folder
C:\>type "c:/temp/temp/test.txt"
The system cannot find the file specified.
C:\>REM Forward slashes still fail with TYPE
C:\>type "c:/temp/temp\test.txt"
hello world
C:\>REM But forward slash still works as long as last slash is back slash
C:\>dir "c:/temp/temp/*"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
File Not Found
C:\>REM Again, forward slashes fail, but directory path is listed properly
C:\>dir "c:/temp/temp/"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM <DIR> .
05/09/2012 09:58 PM <DIR> ..
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
2 Dir(s) 337,001,615,360 bytes free
C:\>REM And again it works if no file is specified
C:\>dir "c:/temp/temp\test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp\temp
05/09/2012 09:58 PM 13 test.txt
1 File(s) 13 bytes
0 Dir(s) 337,001,615,360 bytes free
C:\>REM Again forward slashes work as long as last slash is back slash
Here is a case that clearly demonstrates a bug.
c:\>dir /s /a-d temp
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 08:01 AM 13 test.txt
1 File(s) 13 bytes
Directory of c:\temp\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
Total Files Listed:
2 File(s) 23 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM Note the different file sizes found in each directory
c:\>dir "c:/temp/test.txt"
Volume in drive C is OS
Volume Serial Number is EE2C-5A11
Directory of c:\temp
05/10/2012 07:57 AM 10 test.txt
1 File(s) 10 bytes
0 Dir(s) 337,325,191,168 bytes free
c:\>REM It is listing the wrong file!
One can debate whether Windows CMD is "supposed" to support forward slashes. But that last result is a bug! Even if there is operator error in using a forward slash, Windows should not give that result.
Wie had some strange behaviour with slash to and we traced it down to the fact, that a path with a leading slash isn't seen as an absolute path, so
C:\>cd /temp
C:\temp>rem works we are in the root directory
C:\temp>cd /temp
Das System kann den angegebenen Pfad nicht finden.
C:\temp>rem does't work but
C:\temp>cd \temp
C:\temp>rem \ indicates absolute path
C:\temp>cd ..
C:\>cd /temp
C:\temp> cd /ca
C:\temp\CA>rem qed
Perhaps it explains also the bug stated above - im not clear, at which directory the commands are executed.
I am not sure why the '/' is working in PS. Back to the history, DOS was based on UNIX and it is a small set of UNIX. In UNIX the path separator is '/', while in DOS it is '\'. I worked on some Windows and DOS apps before. In order to convert some UNIX pattern like commands or path and make sure they are valid DOS command or path, I wrote a small converter to transform '/' to '\' like this:
string fileNameFromWeb;
...
string windowsFile = fileNameFromWeb.repleace("/", #"\");
You may add this feature to tolerant '/' in your app in case of accessing to files in Windows. I guess PS may have this type converter to allow command or path using '/' or '\', or Windows will take '/' in file name.

How to filter txt file by string with using batch

I know this kind of problem will be very easy for you but I am total beginner in batch mode.
The question is how bat file code should look like to generate a new txt file from specific one .
for example .
I have report txt
Displaying status for license file: 7788#Server01
License Server: server01
License In Use Free
------- ------ ----
Design* 1 6
(user1#host1) 127 server01 7788 4402
Assembly* 0 4
Pro 0 15
AdvSE 2 3
(user2#host2) AdvSE server01 7788 2706
(user3#host3) AdvSE server01 7788 1503
SingleSite_License 1 3
(user4#host4) SingleSite_License server01 7788 2003
Intra_CLIENT_License 1 4
(user2#host2) Intra_CLIENT_License server01 7788 2003
CAD 1 32
^(user2#host2) CAD server01 7788 501
* = License Extensions - Available only on startup.
^ = Borrowed License.
Press any key to continue . . .
What I want to have in new file from this one are only lines :
SingleSite_License 3
Intra_CLIENT_License 9
but both SingleSite_License and Intra_Client_License should be taken from 1st found string -other are not necessary.
report txt can be different and licenses can be showed in different order .
if it not possible - other solution can be just only Free value will be written in new txt file - f.e 3 and 9 . so the last string in line which contains specific words
thank you for any tips
Not sure where you got the figure of 9 for the Intra_CLIENT_License (souldn't it be 4?), but this script will print out the free licences for those two product codes:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,3" %%a in ('type infile.txt') do (
if "x%%a"=="xSingleSite_License" (
echo %%a %%b
)
if "x%%a"=="xIntra_CLIENT_License" (
echo %%a %%b
)
)
endlocal
I basically cut and pasted your transcript into the infile.txt file and ran this script to get:
SingleSite_License 3
Intra_CLIENT_License 4
(you can replace type infile.txt with whatever command you need to generate that output).
Breakdown:
The setlocal just sets up cmd.exe to allow extensions and delayed expansion of environment variables. All my scripts start with this since it's so useful.
The for loop basically processes one line at a time, grabbing tokens 1 and 3 (a and b) from each.
Then it's a simple matter of checking a for the required licence values and, if it matches, outputting that along with b.
Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.
Download UnxUtils and use the Unix stream processors. Might be possible to do with Powershell too.

Resources