pushd \\network\path returns CMD does not support UNC paths as current directories - windows

I was using a batch file to access some files from network.
I am using
pushd \\Network\path
to navigate to the networked directory to initiate some scripts
My bat file was working perfectly fine till this morning I saw "cmd returning CMD does not support UNC paths as current directories".
I have seen this error message when using cd instead of pushd to navigate to a network directory but I can figure out why I am getting error for a previously working bat file.
And I did make sure that network location was online and accessible other way round.

Be sure to also check that you haven't just run out of drive letters. If you have a rogue script that isn't using popd after it's done with the drive, or is crashing before it gets to popd, you can end up with a bunch of garbage mapped drive. Easy to check as they'll show up in net use and "My Computer". For some reason cmd will give this cryptic error (CMD does not support UNC paths as current directories.) instead of telling you it ran out of drives to map to.

From pushd /?:
If command extensions are enabled the PUSHD command accepts network
paths in addition to the normal drive letter and path. If a network
path is specified, PUSHD will create a temporary drive letter that
points to the specified network resource and then change the current
drive and directory.
Do any mapped drives show up in net use after pushd is executed?
If you're not getting some kind of network authorization error, make sure command extensions are enabled (I'm not sure why they wouldn't be.)
cmd /x will enable extensions for the current CMD session. Try that prior to executing your batch script.
The "main switch" is in HKEY_CURRENT_USER\Software\Microsoft\Command Processor.
EnableExtensions should be (DWORD) 1

Related

Copy files from One Server to Another Server using Batch Command - Windows machine

How to copy files from one server to another server(VM) using a windows batch command. ?
I have used below command
syntax : xcopy \\source_path \\serverIP\Destination_path /s /a /d
example : xcopy \\c:\repo\testproject \\10.101.101.11\C:\test\project /s /a /d
I'm getting the below error "Invalid drive specification" as of now.
Do I need to give credentials for accessing the VM ? If yes then where and how?
I have checked the destination path is correct.
Is there any other command should be used in that case ?
Do I need to give credentials for accessing the VM ? If yes then where and how?
If the passwords on the source and target machine are the same then no credentials are necessary, otherwise yes they will need to be provided. Two solutions you can consider:
Use "runas" and specify the credentials there
Use the temporary network drive solution proposed in the comment by UserPo41085. This solution uses the "net view" command which has credential parms.
I have checked the destination path is correct.
example : xcopy \\c:\repo\testproject \\10.101.101.11\C:\test\project /s /a /d
Both paths in the example provided are a mixture of standard "DOS" paths and UNC paths. UNC paths reference a share name and not a disk letter.
The example in the link below copies a file on the local machine called zz_yuv.png to a machine called "ws9" which has a share called "c9.system" and the share is mapped to the root folder of the c: drive on ws9. If you are running an account which is a member of the administrators group you can use the admin shares...(admin$, c$ etc.)
xcopy example
Is there any other command should be used in that case ?
Robocopy is built into the later versions of windows. It does come with a learning curve but is much more robust than xcopy. Just as a note, robocopy will be under the same types of credentialing restraints as xcopy - it just has more and better features for copying.

Batch | Why can't a script running in admin access other scripts? [duplicate]

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

UNC Path within a programs command line batch file

I am using a great little program text2folders and I have been able to make it run as a right click context menu in windows 10. I have it working perfect for drives with drive letters but it is failing when I using it on UNC path shares. I know I am close but struggling to fix this last bit for UNC paths.
When I run on a UNC share it is adding <> around the path and causing the program to fail.
I need ideas how to either remove the <> or stop them from being created in the first place. Here is my registry entry and my batch file for the command line.
Any suggestions would be greatly appreciated!
My Registry entry
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\shell\cadastralfolders]
#="Create Cadastral Folders"
[HKEY_CLASSES_ROOT\Directory\shell\cadastralfolders\command]
#="cmd.exe /s /k pushd \"%V\\\"&call \"\\\\server2\\data structured\\PROJECTS\\CADASTRAL NETWORK\\Text2Folders\\cadastralfolders.bat\" \"%V\" \"exit\""
My batch
#echo off
start "" "\\SERVER2\Data Structured\PROJECTS\CADASTRAL NETWORK\Text2Folders\Text2Folders.exe" "%cd%" "\\SERVER2\Data Structured\PROJECTS\CADASTRAL NETWORK\Text2Folders\cadastral folder stucture.txt" -q
exit

the installation package could not be open batch file

I've been working on a batch file all day, that I can't get to work open through GPO (another day, another question). So I decided to do it manually with every computer. I have two exe's and one MSI. The exe's work perfectly fine. They get installed, and it all works out. The MSI, however, doesn't. It gives me the error: the installation package could not be opened. Verify that the package exists and that you can access it, or contact the application vendor to verify that this is a valid Windows Installer package.
Now when I go to the network share and use it from there, it works perfectly fine. So there must be an issue with my code.
Here's the code:
#echo off
IF NOT EXIST "C:\Program Files (x86)\Citrix\ICA Client\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\1\"
.\CitrixReceiver-4.4.1000.exe /silent
)
IF NOT EXIST "C:\Program Files (x86)\triCerat\Simplify Printing\ScrewDrivers Client v4\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\"
msiexec.exe /i ".\Screwdriver.msi"
)
IF NOT EXIST "C:\Program Files\Cloudwerx\CloudwerxPlugin\" (
pushd "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\3\"
.\cloudwerx-setup.exe /silent
)
pause
Any help would be greatly appreciated, thanks.
I am guessing that your problem is the distinction in powershell between the current location (set by the pushd command) and the working directory (unaffected by the pushd command). You can see the working directory of the powershell process using the [Environment]::CurrentDirectory property:
# C:\> [Environment]::CurrentDirectory = "c:\"
# C:\> [Environment]::CurrentDirectory
c:\
# C:\> pushd C:\Temp
# C:\Temp> [Environment]::CurrentDirectory
c:\
# C:\Temp> Get-Location
Path
----
C:\Temp
WHat is probably happening is that msiexec.exe is using the working directory (i.e. [Environment]::CurrentDirectory) and not the current powershell location at invocation. I would just specify the full path to msiexec:
msiexec.exe /i "\\KOPI-DC01\ACCURO Cloudwerx\ACCURO\2\\Screwdriver.msi"
MSI installation packages build with an older WIX utility would throw the error whenever installation was attempted from a batch script that was accessed on a shared drive using UNC path instead of a mapped drive letter. On the other hand whenever the batch file was executed with a mapped drive letter the installation would work normally.
I'm not blaming WIX here because I'm not certain whether they are responsible. I'm just describing symptoms here. It might just be the result of invoking plain vanilla Windows batch script that in turn executes msiexec with a bunch of command line parameters.

Cannot fully delete ProgramData from Windows 8 installation within WinPE

I have a script that runs in WinPE that takes a system drive with Windows installed and deletes everything off of the drive (keeping the filesystem intact).
When dealing with a Windows XP/Vista/7 installation it functions properly. attrib -S -A -H -I -R /S /D \ is run, and then everything is deleted.
However, within Windows 8, I run into an "Access Denied" error. For some reason, even as the SYSTEM user within WinPE, I can't edit the directory C:\ProgramData\Microsoft\Windows\LocationProvider. I can't use attrib to set attributes, I can't delete it - I can't even cd into it! dir /a just returns File Not Found.
Using rmdir /S /Q gives me the "Access Denied" error.
Assuming that the problem is related to permissions and/or ownership, you can work around it using the built-in robocopy tool - luckily, this is included in Windows PE.
First, create an empty directory, e.g., x:\empty and then run
robocopy /e /purge /b x:\empty c:\
The /b flag tells robocopy to use backup mode, which bypasses security.
Had the same problem. You need to take ownership first, for example using takeown.exe. Then fix permissions, for example using icacls.exe. Then proceed as you wish with copy, move, delete.

Resources