Access lines from a text file on a network - batch-file

I need to create a batch file that reads computer names and saves in a text file on a network location. I have tested the loop below and it works locally by changing the location to read my desktop.
Putting the .txt file there, and had it echo the contents. Works fine. When I change the location to a network one, it says the file cannot be found.
for /F "tokens=*" %%A in (\\lanlocation.com\filepath\filex\IT\"IT Scripts"\Deployment\computer.txt) do pushd \\%%A\c$
pause
I've run into issues before with a space in the network path name causing issues, but I don't believe that's the case here as the error reads off the entire correct path before saying "file cannot be found". Lastly...yes. The file is there and I'm referencing the right name.
Also, if anyone has a good resource for some batch information, I'd be very appreciative if you would share a link or name of a book.
Thanks!

Related

Batch File Calls VB Script & Passes Filename as String Only

I have a batch file that calls a VB Script file. This VB Script comes from MS Office Suite and is located in the Installed Folder. My file calls the VB Script and pipes it to a file using the > symbol. This works if your redirect is a string. I was wondering how to redirect to a file using a variable.
I'm only including a small fraction of the code showing how I want it to work.
This, as written, works. However I want my batch file to use a variable instead of the text in quotes. In fact, my batch file creates the path depending on where the batch file is located.
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > OfficeStatus.txt
Echo.
When written as shown below, it does not work:
::Create file and display on screen
Echo Writing Information to File: "OfficeStatus.txt"
Call cscript ospp.vbs /dstatus > %_sLogFile%
Echo.
I don't know if the issue is because of the redirect not able to handle a variable or not. An example of what might be in the variable:
_sLogFile=E:\UserName\Documents\Status\OfficeStatus.txt.
Any thoughts would be great.
The variable, by the way, correctly contains the path and filename... so that's not the issue.
it does not work means did not do what I expected and unfortunately does not tell us what it did do.
Did it crash the system? Create an error message (If so, what message)? Create an output file in an unexpected place??
Personally, I'd suspect what you have concealed as username. Without knowing precisely what that text is, we start guessing, which isn't a logical approach. It doesn't have to be the real username, substitute Fred Bloggs for the real name to mask it if necessary.
I believe that username may either be %username% to retrieve the name from the system, or a real literal. The problem with this is that such names often contain spaces, you'd need to "Enclose the full filename in quotes".
See - had you told us _sLogFile=E:\Fred Bloggs\Documents\Status\OfficeStatus.txt it would all have been a lot clearer - presuming that my guess as to the cause of the problem is correct.
Perhaps you should look for files named like E:\Fred which is where the expected output may have ended up.

Why can FOR not find a specified file when used to read all lines from within my batch script?

I have a few log files that my batch script needs to be able to read and parse.
I've run into a bit of a problem, where my FOR loop can read most files, but cannot read a specific one. If I run in a command prompt window
for /f %Z in (N151204.log) do echo %Z
it works great, but exactly the same syntax on a different file:
for /f %Z in (IN151205.log) do echo %Z
Gives me:
The system cannot find the file IN151205.log.
I can read file IN151205.log with
more IN151205.log
without a problem. So it doesn't seem to be a permissions issue, or read-locked as far as I can tell.
Why is the for loop having issues with this particular file?
The error message of command FOR is caused by the fact that FOR command opens a file for reading always using CreateFile with FILE_SHARE_READ AND FILE_SHARE_DELETE.
The log file IN151205.log is obviously written at the moment by another application with having a write lock established on the file. Therefore the file access by command FOR fails with a sharing violation because of FILE_SHARE_DELETE.
MORE has success to open the file with write lock set and outputs the lines as it really only reads the file and using a different method. MORE uses CreateFileMapping using PAGE_READWRITE to read and output the file contents page wise which works completely different than CreateFile and can be used also on files currently locked by another application as mentioned in Remarks section with:
It is not required, but it is recommended that files you intend to map be opened for exclusive access.
PAGE_READWRITE allows also read access on a file currently allowing only read access according to sharing mode.
See also the Microsoft articles Generic Access Rights and File Security and Access Rights.
The error message output by FOR on access to a file fails because of a sharing violation is obviously not good as being misleading for users.
EDIT:
Reading a write-locked file from within a batch file line by line can be done with
for /f %%Z in ('more IN151205.log') do echo %%Z
and with
for /f %%Z in ('type IN151205.log') do echo %%Z
Thanks Aacini and Squashman for this additional information.

User CMD Batch Interaction

I am creating a batch file that will install files into a certain location on a computer. this could be used for various things such as game mods, folder backups, etc. If i am trying to have the user type in the path they want the file to be sent to. How would I do that? I would like to have that path pasted into a .txt file, that way i can access it later in the program. I have tried the ">" and it works on putting the path into the file. How do I extract that information. "Direct Question" How can I take user input, and use it to paste files somewhere else in the computer using the user's path that was specified.
How many questions do you want for your money?
To input a string:
set /p "installdir=Install in which directory? "
At any point thereafter, you can use %installdir% to access the directoryname input.
If you need to save the directoryname in a file, then use
>"c:\wherever\you\want\filename.txt" echo %directoryname%
and to read it,
for /f "usebackqdelims=" %%a IN ("c:\wherever\you\want\filename.txt") do set "dirread=%%a"
or
set /p "dirread="<"c:\wherever\you\want\filename.txt"
and "pasting" or probably copying,
copy /b "x:\directory from\filename.whatever" "%dirread%\"
to copy the file "filename.whatever" from directory "x:\directory from\" to directory in the environment variable dirread as read by one of the previous two methods or substitute directoryname for dirread to use the directory name originally entered.

Copy a file from network share system cannot find path specified

I have a seriously weird problem, and I suspect it has something to do with the length of the filename - but I cannot alter it (for now).
I'm trying to copy a file from a network share to the users local %temp% with a logon script. The reason for this, is that each user needs to have an individual copy of the file when opening it so that the next users doesn't get a "file is locked" message.
If I run the command manually in a CMD window, the file gets copied, but if I place it in a login.bat-file on the domains netlogon-folder and tries to run the bat-file, I get "The system cannot find the path specified."
This is the actually command:
COPY /Y "\\SERVER\Felles\Administrasjon\Customer KS-manual\Kvalitetshåndbok\2000 org. kart\2 0 B 01-14 Customer Name Arbeids-Prosess-flyt_NO.odg" %TEMP%\Kvalitetshaandbok.odg
So as I said - if I type that in CMD, the file gets copied. If I place it in \customer.local\NETLOGON\login.bat and run the BAT-file, I get "The system cannot find the path specified."
I also tried earlier to just open the file, and that only worked if I opened \customer.local\NETLOGON\shortcut.lnk and shortcut.lnk pointed to the UNC-path of the file. The problem is that I cannot copy a file using a shortcut.lnk AFAIK.
My main object is to have each user get a unique copy of the file and opened on logon.
Is the problem isolated to the length, or is there something else I miss out on?
Try "%TEMP%\Kvalitetshaandbok.odg" with the double quotes because the username can contain spaces etc, which trickles down to the %temp% path.
There are also non-latin characters in the path and the code page being used may need to be altered to match the character set.

I am trying to start an office program on a flash drive with a batch file

I am putting Kingsoft office on my flash drive, and I want to use a batch file to start the applications because the paths are not easily accessible, I cannon create a .lnk file because the path varies by computer because it may be plugged into a different port. Here is my batch file code, could somebody give some suggestions on how to make this work. Thanks in advance...
set "path=%~dp0"
start %path%office6\wpp.exe
The second line is the problem, the program won't start the program. Thanks!
cd /d "%~dp0"
start "" /b wpp.exe
I think some of the directory names in %path% contain spaces and since %path% is not enclosed within ""( double quotes), the script is unable to find the exe .
You may also want to include a log file so that it becomes easier to debug in case of any errors.
Try this:
set baseFolder=%~dp0
start "%baseFolder%office6\wpp.exe" > "%baseFolder%batchRunLog.log"

Resources