Batch - error in findstr - batch-file

My objective is
Map a network folder
Find a file on per-determined date with specific registration number
Copy the file to a specific location
Here is my code:
set "folder="
set "date="
set "No="
set "fileName="
echo:
set /p folder=Please enter the folder name
echo:
set /p date=Please enter report date yyyymmdd =
echo:
set /p No=Please enter registration number =
echo:
net use Q: \\%folder%\d$\h
net use R: \\%folder%\d$\p
CD Q:\%date%\0\ | findstr /i %No% "*.*"
echo:
set /p fileName=Paste filename to resend =
echo:
copy %fileName% y:
echo:
echo Copy Complete
echo:
Here is the output:
Please enter the folder you want to connect/reconnect = 0714
Please enter report date yyyymm = 201407
Please enter registration number = 74471958
The command completed successfully.
The command completed successfully.
FINDSTR: Cannot open NTUSER.DAT
FINDSTR: Cannot open NTUSER.DAT.LOG
Paste filename to resend =
I am currently running the batch from D: and I have copy findstr.exe to the same folder from which the batch file is running.
Currently I am really out of ideas and hope someone can point me on a right directions.

Your problem is this:
CD Q:\%date%\0\ | findstr /i %No% "*.*"
The pipe | takes the output of the previous command and feeds it to the following command.
The output of a cdcommand is empty (or "the system can not find this path").
So this is not, what (I think) you need.
I think, you want to go to Q:\%date%\0\ and search there for files, that contain %no%.
To do this, use:
cd /d "Q:\%date%\0\"
findstr /i %No% "*.*"
You need the /d parameter with cd to change to another drive (or use pushd instead of cd)

The problem with needing findstr.exe depends on precisely how you are executing this batch. This executable should be in c:\windows\system32 and that directory should be in your path. I'd echo %path% and pursue it from there.
Next problem is %date%. This is a magic variable and contians the current date. It can be overridden by a set command, but if it's set to nothing as you have done in you code, then it will return the current system date in the current user's format.
AFAIAA, CD produces no output; it merely changes directory.
I suspect you are using cygwin which provides different definitions for some commands.

Related

If else statement in bat

The code below checks if there is a folder called Made_files if so it will cd to that folder and make a new file using variables
Else it will make a folder called Made_files THEN cd to that folder and run the code but at the moment all it does is flash up then close instantlly can someone help
if EXIST Made_files (
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
) else mkdir Made_files
cd Made_files
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
echo #echo off > %name%.%Filetype%
echo color 0a >> %name%.%Filetype%
echo ___
pause
#ECHO OFF
SETLOCAL
:: Ask for filename and type
set /p name=Name of file:-
set /p Filetype=Type of file(txt, bat js):-
:: Required file to be located in "Made_files"
SET "subdir=Made_files"
MD ".\%subdir%" 2>NUL
CD "%subdir%"
(
echo #echo off
echo color 0a
)> %name%.%Filetype%
echo ___
pause
GOTO :EOF
What your code does:
If the subdirectory already exists, switch to that directory, ask for the filename&type, then create a new file using the requested data. Otherwise, create the directory.
THEN
Change to the subdirectory again (so if made_files already existed, try to change to .\made_files\made_files), ask for the filename and type and create the file.
===
BUT Please read Stephan's DELAYEDEXPANSION link. Since you are not using delayedexpansion, when the if statement is parsed (and the if statement continues until the end of the else clause) the values of the variables name and filetype are replaced by their values when the statement was parsed (~verified), NOT as they were input, so the destination filename will be nothing.nothing , which is "." - a directory-name (sort-of).
It's likely that this is the source of the drama.
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.
===
So : the code I suggest:
Asks for the file name and type.
Attempts to make the directory. If it already exists, this will generate an errormessage which is suppressed by the 2>nul.
switches to that subdirectory.
Creates the file.
Note that if the echoes are surrounded by parentheses, then their output is combined and directed to the file, saving a plethora of redirect-to-file clauses.

Getting the standard output from CD into a file within a loop

Using some components I found here, I have built a batch file to loop through a directory tree starting from the directory where the batch file runs.
The batch file works as expected but I need to capture the output from the cmd.exe command CD into a file that I created earlier in the run.
The problem is that if I attempt to redirect the standard output into the .txt file I only see the first found directory.
I have found some code which uses PowerShell, to pull the listing from the Command Prompt screen, but to me this is inelegant, (although it seems to work).
I have read the material on setlocal enabledelayedexpansion but it appears to be above my paygrade as I haven't been able to make it work.
The working code is below, with a Remark where I think the export to the .txt file should go.
Help would be appreciated.
Rem Recursively Traverse a Directory Tree
Rem Notes:
Rem "For /r" command can be used to recursively visit all the directories in
Rem a directory tree and perform a command in each subdirectory.
Rem In this case, save the output to a text file
Rem for /r = Loop through files (Recurse subfolders).
Rem pushd = Change the current directory/folder and store the previous folder/path for
Rem use by the POPD command.
Rem popd = Change directory back to the path/folder most recently stored by the PUSHD
Rem command.
#echo off
CLS
echo.
echo.
Rem FirstJob - Generate a date and save in the work file.
Rem Grab the date/time elements and stuff them into a couple of variables
set D=%date%
set T=%time%
set DATETIME=%D% at %T%
Rem OK. We now have the date and time stuffed into the variable DATETIME
Rem so now stick it into our work file along with a heading.
Echo List of Found Directories > DirList.txt
Echo %DATETIME% >> DirList.txt
echo. >> DirList.txt
echo. >> Dirlist.txt
Rem SecondJob - Do the looping stuff and save found directories to file.
Rem Start at the top of the tree to visit and loop though each directory
for /r %%a in (.) do (
Rem enter the directory
pushd %%a
CD
Rem ------------------ direct Standard Output to the file DirList.txt -----------------
Rem exit the directory
popd
)
: END
Rem All finished
Echo Done!
exit /b
The additional lines of code when added to the above scrip before the :END marker. which did produce the wanted output were:
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^a')
powershell -c "$wshell = New-Object -ComObject wscript.shell; $wshell.SendKeys('^c')
powershell Get-Clipboard>>DirList.txt
The problem is your pushd command, because you change the current directory, the file dirlist.txt must use an absolute path, else you create it in every pushed directory.
I used %~dp0 here, it's the path of the batch file itself.
You could try
cd >> %~dp0\dirlist.txt
Or just
echo %%a >> %~dp0\dirlist.txt
Or you could use a single redirecton of the complete bock
(
for /r %%a in (.) do (
pushd %%a
echo %%a
popd
)
) > dirlist.txt

Kodi - open .strm files by running a .bat / VBScript

I have legal .strm files of different TV Shows in a folder named TV Shows. Each strm file of each episode is stored in different subfolders.
I would like to run a certain VBScript before these strm files are played.
Then I have a different folder named MOVIES. Again, I would like to run a VBScript before these strm files are played. But this VBScript is a different one.
How would I do that?
Platform is Windows.
Thanks!
If you're looking to open or do something to each file based on extension, An easy way to do this is to use an for loop with /R to search all sub-directories for an *.strm file.
#ECHO OFF
SET "LOC=C:\Folder"
CD %LOC%
FOR /R %%A IN (*.strm) DO (
Rem | Do something with each file.
Start cmd.exe /C "notepad.exe "%%A""
)
Echo No More Items Found!
pause
goto :EOF
%%A will return the file path.
Start cmd.exe /C "" will start a CMD command. (Replace with your code)
notepad.exe "" will open the file (Used as an example)
EDIT:
So you're looking to check if the file exists then if true run the script? Bellow should solve that.
#ECHO OFF
If exist (C:/Path/file.strm) (
Rem | File exists, run script
Echo Do something
)
GOTO :EOF

Create Batch File that prompts user for File path in DOS 6.22

I am trying to create a .BAT file in DOS 6.22 that will copy the contents of a floppy disk in A: over to C:\ and then set the folder created as a system variable. I tried using something like "SET /P VARIABLE=Enter a path" however DOS will just add "/P VARIABLE" as a variable with the value of "Enter a path" so using the /P isn't an option as /P wasn't a switch in DOS 6.22
I tried using something like a for loop to set a variable to the file however where I hit a speed bump is that I have no idea what the folder is going to be called in drive A:\ as it will change all the time but only ever contain one folder, so basically I am just trying to find a way a way to copy the first directory found in drive A over to C:\ and set that as a system variable. As once the user is done making changes I will have to copy that folder back over to A:\ and overwrite the old files so it can be stored on the network once changes have been made.
I did try experimenting with some If/for statements through a .BAT file but I didn't have much luck with theses, if anyone could point me in the right direction that would be awesome.
At this point I'm probably making this way more complicated than I have to.
something like this should work too:
#echo off
:INPUT.BAT puts what is typed next in environment variable INPUT
set input=
echo INPUT.BAT
echo Type in something and press [Enter]
fc con nul /lb1 /n|date|find " 1: ">temptemp.bat
echo :Loop>>enter.bat
echo if not (%%input%%)==() set input=%%input%% %%5>>enter.bat
echo if (%%input%%)==() set input=%%5>>enter.bat
echo shift>>enter.bat
echo if not (%%5)==() goto Loop>>enter.bat
for %%x in (call del) do %%x temptemp.bat
del enter.bat
echo The string you just entered:
echo %input%
echo has been stored in an environment variable named INPUT
:End
I ended finding a solution after doing some more research from what #Squashman linked. Turns out there was a breakdown in communication and this wasn't even the original issue that the user had (a simple way to copy the files off A:\ and all that)
I used the following.
echo Type "set myvar="name of the folder" replacing name of the folder with
echo the name of the folder containing the files on A:\ example if you were
echo on "example" you would type: set myvar=example
copy con answer.bat
echo Type the words "set myvar=" (don't type the quote marks)
echo and then immediately after the = sign, press Control-Z then enter
call answer.bat
mkdir C:\%myvar%
xcopy A:\%mvar% C:\%myvar%
DEL answer.bat
This is a modified version of a guide I found here. http://www.pement.org/sed/bat_env.htm#4dos
Hopefully this is able to help someone, this isn't pretty by any stretch of the imagination but it worked.
I think this would be easier for your users if they actually need to only copy one directory.
#echo off
if "%1" == "" goto syntax
md C:\%1
xcopy/E A:\%1 C:\%1
goto end
:syntax
echo Please input a directory after %0
:end

How to write Batch Script to Change Directory to WILDCARD folder based on user input in order to copy a

I want to be able to copy the zip file over from a folder that will sit on desktop. The folder will have a different name every week. We will process it and delete it. I want to be able to use a user input to find any folder on my desktop that has that name in it. Once found, I want to be able to go into that folder and copy the zip file that is in there to a folder called PROCESSED LINKFILES. Here is the script. The first part works fine but I can't get the the user input to find the correct folder by using the wildcard feature * . Please help:
usermessage.vbs
ECHO WScript.Echo InputBox( "Please Enter the name of the store", "Store Name", "" )
FOR /F "tokens=*" %%A IN ('CSCRIPT.EXE //NoLogo usermessage.vbs') DO SET StoreName=%%A
ECHO Store Name is %StoreName%
DEL usermessage.vbs
copy C:\Users\ADMINISTRATOR\Desktop\*%StoreName%*\*.zip C:\Users\ADMINISTRATOR\Desktop\PROCESSED_LINKFILES
read HELP FOR and then try at the command line this double FOR loop
for /d %d in (\temp\f*) do #for %f in (%d\f*) do #echo %f
as you see, it will loop over all the f* folders and for each of them if will loop over all the f* files.
So, adapting this simple strategy to your requirements and giving it an appropiate BAT syntax, you may begin trying this...
#echo off
set storename=%1
set myfolder=C:\Users\ADMINISTRATOR\Desktop\
set dstfolder=%myfolder%\PROCESSED_FILES
set myfiles=*.zip
for /d %%d in (%myfolder%\*%storename%*) do (
for %%f in (%%d\%myfiles%) do (
echo copy %%f %dstfolder%
)
)
test carefully before removing the ECHO

Resources