I'm writing a batch file in which I need to determine whether or not a particular folder on my hard drive is a Hidden folder. The batch file will then proceed depending on the answer.
I've studied the responses to some similar questions here but I'm still not getting it. Any help with this part of my batch file appreciated.
Note: It's the same folder all the time but the System attribute changes from time to time.
Thanks!
The output of attrib command is in the format 'A_SHR_I', so we will test if the fifth character in output is an H to determine if the folder is hidden
attrib "c:\Myfolder" | findstr /r /c:". .H" > nul
if errorlevel 1 (
echo Folder is NOT HIDDEN
) else (
echo Folder IS HIDDEN
)
This is the behaviour in a Windows 7 Spanish installation. Try on your systems and adapt as needed.
Related
We run NetSuite for our shipping software and use Zebra ZP450 printer to print the thermal label.
The software downloads a .zpl file which I have assigned to run with a batch file, script here:
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
I have installed this on probably 20 or more computers without an issue. But the last two will initiate the batch file but not print.
I was able to hit pause on the batch file and here is what I got:
C:\users\noah\downloads\net use LPT2: \(ComputerName)\ZebraFedex
The command completed successfully
C:\Users\Noah\Downloads\Copy LPT2
The system cannot find the file specified. 0 files copied
C:\Users\Noah\Downloads>Net use LPT2: /Delete
I've checked my file association, which seems correct since I see the command prompt flash on the screen. I have no idea here...
Working successfully on another computer
Problem on puzzling computer
Here's my batch to transfer file to my venerable TLP2844:
#ECHO OFF
SETLOCAL
FOR %%a IN (%*) DO (
IF EXIST "%%a" (COPY /b "%%~a" \\%computername%\TLP2844
) ELSE (
IF EXIST "%%a.txt" (
COPY /b "%%~a.txt" \\%computername%\TLP2844
) else (
ECHO Neither "%%a" nor "%%a.txt" appears to EXIST
)
)
)
where I have a printer called TLP2844 installed as a ZDesigner TLP 2844 device on USB003.
The problem, as shown by the graphics I've included in your question (saves running off chasing links) is that the label file C:\users\melan\downloads\UPSSHIPPINGLABEL1700448.zpl is being provided to the batch as %1 (first parameter). On the problem installation, no filename (presumably C:\users\Noah\downloads\UPSSHIPPINGLABEL???????.zpl is being provided to the batch as %1.
Therefore it's not a batch problem, but a problem with Netsuite.
From your .pdf, on page 26 :
To install a thermal printer driver on a Windows PC:
...
5. In the Share Name field, enter the name of the printer. For example, LP 2844.
The printer name cannot contain spaces.
So you're told the printer name cannot contain spaces, but the example printer name contains a space....Excellent! (shows my level of confidence in the product, but not actually directly relevant to the problem) :(
Personally, I'd change your batch file to
Copy %1 \\%ComputerName%\ZebraFedex
Perhaps you'd try that after fixing the problem where Netsuite fails to deliver the filename to the batch. Perhaps it's set up to produce .pdfs on the computer in question (That's a wild guess)
In response to reply:
Please change your batch file (temporarily for test purposes) to this:
Echo File to print : "%1"
Net use LPT2: \\%ComputerName%\ZebraFedex
Copy %1 LPT2
Net use LPT2: /Delete
PAUSE
which will report the filename supplied by Netsuite to the batch file on the console window, then keep the window open at the end of the batch until the open window receives a keystroke.
Please report results for both a "working" and a "buggy" installation.
Change .bat file like below, replacing the printer share name with your own. Happened on one of our critical computers that is also the host for FedEx/UPS.
C:\WINDOWS\system32\net.exe use LPT2: \\%ComputerName%\Zebra_ZP500_Shipping_FedEx1
Copy %1 LPT2
C:\WINDOWS\system32\net.exe use LPT2: /Delete
Thank you Noah Schultz for the Pause suggestion for batch file, and to QWERTY for article on Net command issue
Reference
EDIT: To clarify what I'm looking to do is move a few files into a data folder of an application. The application is installed into Program Files but it keeps data inside app data. The folder name looks something like this
76053ADAJSQDUC4975
Problem is that it's unique to every instance of the application installed and for every computer that will be using this batch. So I'm in the directory and it has
1AKDHCI4985HF55GHJKB G5586HJFRUK56885KOQQ
The only way to identify the folders is by a .txt file inside each one called
origin.txt
that shows the file path to the applications installation directory (C:/Program Files(x86)/********) on one line.
I figured I can use a for to loop through, find the the file, and read it. What I don't know how to do, is find the right file, and cd to its directory. The second problem is since this batch file will be used by multiple users, not all of their installation paths are the same. So inside .txt it could be C:/, D:/, Program Files or Program Files(x86) so the only thing useful to me is the last several words. How would I go about being selective like that.
I'm currently traveling so can't answer right away but would appreciate if you guys help me out or point me in the right directions. Thanks
First, if the application is installed with an installer which adds something to Windows registry for knowing itself on next run (update/upgrade/uninstall) which version of the application is already installed and into which directory if installed at all, it is better to evaluate this information in registry instead of searching around in file system. See my answer on Locate if the program exists and set a custom variable in batch windows.
Second, with assuming text file origin.txt can be found anywhere within application data directory of current user containing only once (or last) the string :\ as part of the directory with the program files of the application, the following batch code could be used to get the directory path and make it the current directory.
#echo off
set "ApplicationDirectory="
for /F "delims=" %%I in ('dir /A-D /B /S "%APPDATA%\origin.txt" 2^>nul') do (
for /F "delims=" %%D in ('%SystemRoot%\System32\findstr.exe /C:":\\" "%%~I" 2^>nul' ) do (
if exist "%%~D" (
set "ApplicationDirectory=%%~D"
goto ChangeDirectory
)
)
)
echo Could not determine directory of application.
goto :EOF
:ChangeDirectory
cd /D "%ApplicationDirectory%"
echo Directory is: %ApplicationDirectory%
set "ApplicationDirectory="
The first FOR loop uses command DIR to search in all subdirectories of user related application data directory for the file origin.txt. The inner FOR loop is processed if there is at least one such file found.
The inner FOR loop calls FINDSTR to search in found origin.txt for all lines containing the literal string :\ and processes all found lines.
For each line returned by FINDSTR the inner FOR loop checks if there is a directory (or file) using the entire string of the line containing :\.
Both loops are exited immediately on a positive check on existence of directory (or file) with a jump to label ChangeDirectory with the commands to switch the current directory independent on current drive and echoing the found application directory.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cd /?
dir /?
findstr /?
for /?
goto /?
if /?
set /?
Note: I strongly recommend to run FINDSTR with a better search string or perhaps even a regular expression than just :\ if file origin.txt could contain multiple paths.
I ended up using
set "ApplicationDirectory="
for /r "%appdata%" %%a in (origin.txt*) do find "thewordsiwaslookingfor" <"%%a" >nul && set "ApplicationDirectory=%%~dpa"
Then I used ApplicationDirectory to be able to move some files into that directory.
Which worked perfectly, for what I was trying to do.
I am trying to write a log that a user will create in a batch file, as a small little game, but I can't seem to get the correct directory of the file down. I want to write to this directory:
BatchfileandFolder\subfolder1\subfolder2\ThedataisHere.txt
This is the code I have (I really don't know what I am doing)
Echo Write a piece of text here
set /p UserData=
>>\subfolder1\subfolder2\"ThedataisHere.txt" echo %UserData%
It is essential trying to dig deeper into the directory, but I don't know the exact command, and the help menu for CD, and PATH on the cmd.exe promt don't really help me that much.
Thank you for real human contact, speaking real English
#ECHO OFF
SETLOCAL
SET "logfile=u:\sub folder1\sub folder2\ThedataisHere.txt"
FOR /f "delims=" %%a IN ("%logfile%") DO MD "%%~dpa"
Echo Write a piece of text here
set /p UserData=
>>"%logfile%" echo %UserData%
GOTO :EOF
This method uses a variable logfile so that you aren't forever typing out the name (and avoid the pain if you want to change the names or directories, and the method allows you yo use multiple logfiles easily if you want)
I've deliberately used spaces in directory names to prove the method. The directory gets created immediately after the logfile name is set
append 2>nul to the md instruction line to suppress the directory already exists message
From there, simply use >>"%logfile%" to create the log. The quotes are not required if the filename doesn't contain separators like spaces.
Note that if the first character of the directory specified is \ then the directory is relative to the root, but if it is not then the directory is relative to the current directory on the destination drive. u: is a drive specifier, not a directoryname; I use u: as my test drive. Your choice is up to you.
If the directory structure already exists, it's pretty simple:
#echo off
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
If it doesn't exist already, you have to create it first (tested on Win7 64 bit):
#echo off
if not exist "subfolder1" md "subfolder1"
if not exist "subfolder1\subfolder2" md "subfolder1\subfolder2"
set /p UserData=Write some text here:
#echo %UserData% >> "subfolder1\subfolder2\TheDataIsHere.txt"
Although your description is ample, some important points are missing. It is obvious that you know "How to change the directory that a batch file will output text to", because you use the >> \subfolder\... notation, so this is not your problem. I can only guess that you want to know "How to get the directory where a batch file is located", so you can write to a log file placed two levels below that directory. If this is your problem, then you may use the %~P0 notation, that represent the path of the batch file; that is:
>> "%~P0subfolder1\subfolder2\ThedataisHere.txt" echo %UserData%
Note that the value returned by %~P0 ends in a backslash, so %~P0 must not be separated by an additional backslash from subfolder1; also note that the quotes must enclose the whole path of the file.
If this is not what you want, please carefully describe your real problem. Anyway, try to be clearer in future questions.
I've done some small batch files before, but never any that required file names being read in a folder.
I need a batch file that can sort a folder for me.
I need it to look into a folder, search each text file, if it contains a "string", then move it into an accepted folder, if not, move it into a rejected folder.
Any ideas how to tackle this?
I have used something as follows for finding text before.
find /c "string" file
if %errorlevel% equ 1 goto
I don't know how to make it search each file in a folder, and those file names change on a daily basis.
in your problem statement you said all of these files are .txt files. So I would recommend using a for loop to loop through each of the .txt files in the directory, and then use findstr to search for the intended string. The code I posted below will work in such a manner. Essentially it changes the directory to the desktop (or wherever your files are located), then loops through each .txt files and searches for the string. The findstr output is redirected to nul (for faster processing) and if this is successful, the .txt file is moved to C:\Users\Desktop\SuccessFolder, otherwise this file is placed in C:\Users\Desktop\FailureFolder. (Keep in mind the search "string", original location, and 2 folder locations still need to be set) I believe this code should work for what you are trying to accomplish. Let me know if any changes need to be made to the code =]
#Echo off
cd /D C:\Users\Desktop
FOR %%a in (*.txt) do (
findstr /C:"string" "%%a" >nul && move "%%a" "C:\Users\Desktop\SuccessFolder" || move "%%a" "C:\Users\Desktop\FailureFolder")
This isnt really a coding question. I need to put all my files into individual directories so each file has its own directory with the name based on the filename.
So before i go make an application to do this, does anyone know any software that can do this? like Automator or something.
No need to build an application. This simple one liner run from the Windows command line will move each file in a directory into a sub-directory based on the root name of the file.
for %f in (*) do #(md "%~nf"&move "%f" "%~nf")>nul 2>&1
Two files with the same base name but different extensions will be moved into the same directory. For example, "test.txt" and "text.doc" will both be moved into a directory named "test"
Any file without an extension will not be moved.
If you want to run this from a batch file, then
#echo off
for %%f in (*) do (
md "%%~nf"
move "%%f" "%%~nf"
) >nul 2>&1
You requirements were not very clear. If your requirements differ, then the script can probably be modified fairly easily to suit your needs.