Files copied with batch script disappear from folder once terminal is closed - windows

Right now I'm trying to work on a batch script that will take files from one directory, rename them, and copy them into another directory. It's incredibly simple but I'm running into a strange problem where the script will copy and rename files appropriately, but as soon as the terminal closes and the script terminates, the files disappear as if nothing was used.
REM displays as mmddyyyy
set copy_date=%date:~4,2%%date:~7,2%%date:~10,4%
set file_path=C:\wkspace\dir\files\
set archive_path=C:\wkspace\archive\files
Call :copy_files_to_archive
pause
:copy_files_to_archive
set dir_list=\dir1 \dir2 \dir3 \dir4
for %%i in (%dir_list%) do (
if %%i==\dir1 (
copy "%file_path%%%i_filename_%copy_date%.xml" "%archive_path%\sub_dir%%i_filename-ARCHIVED_%copy_date%.xml"
)
if %%i==\dir2 (
... the same code is repeated for the rest of the files through \dir4
)
)
EXIT /B 0
Like I said earlier, the files are showing up in the target directory and they're renamed. But when I hit any key in the terminal to close it, all of the files just disappear. They aren't hidden or anything so I'm stumped.
Just a note: I stripped this code down to only focus on one file because it's repetitive. Focusing on the one file should fix my problem
All help is appreciated! Thanks!!

Related

Detect a Missing file based on its Date

I need to manually monitor a random windows-based backup program for failures and the only way to do this is via checking for the daily backup file it makes.
A batch file would be simplest for me to implement. It would need to scan a directory for a certain file type and then do X if a file doesn't exist that was created/modified yesterday.
C:\backups\random-named-file.tib 27/03/2017
C:\backups\random-named-file.tib 28/03/2017
C:\backups\random-named-file.tib 29/03/2017
So if a random-named-file.tib wasnt created or modified yesterday, it means the backup failed and I would write output to the console saying "Backup Failed"
Does anyone please have any ideas ?
EDIT :
To clarify, the backup files are coming from a custom application that has no way to check for missed backups. The batch file solution I'm looking for will be integrated into our RMM platform, which will take the results of the batch file and raise a support desk ticket for our Service desk to action.
The backup application will apply its own retention policies and delete any backups older then 4 days.
I'm just trying to detect if a backup was missed the previous day. The filenames are variable, the only constant is the file extension ( *.tib)
This backup app doesn't write eventlogs, nor does it create its own log files I can parse for Failure messages.
1) I would suggest you, go through this article
four-ways-to-manage-windows-server-2008-backup-on-multiple-servers
2) There is a windows scheduler which can act according to your situation
Window scheduler Wiki
3) You can write scripts which alert you whenever it fails
4) I have found a Solution for your similar kind of problem
make-windows-task-scheduler-alert-on-fail
TL:Dr --> see step 4
I manged to do what you asked. On a quick note, since it's Windows backup program there should be a way to automatically alert when backup fails.
Note about this example: I did this in a specific folder structure. If you want to change the location of batch file, second line of code inside batch file needs to be updated with appropriate path. In this example the path is relative, but it's easy to change, so I leave that up to you.
Here is my folder structure:
\---batch # Folder with batch file
backups.txt # check_backups.bat creates this file
check_backups.bat # Batch file that does the job
| filename1.tib
| filename2.tib
| filename3.tib
| filename5.tib
Here is the code of batch file (check_backups.bat):
#echo off
dir ..\* /a-d /b > "%~dp0\backups.txt"
set count=1
setlocal EnableDelayedExpansion
FOR /F "delims=*" %%A IN (backups.txt) DO (
SET fl=%%~nA
IF !fl:~8! NEQ !count! (
echo Backup Failed: Missing file is 'filename!count!.tib'
goto failed
)
SET /A count=!count!+1
)
echo All is good
:failed
pause
If filename9.tib is missing you will get a message: "Backup failed: Missing file is filename9.tib".
If everyhing is good you will get a message: "All is good".

bat/cmd file does not return back

I want to launch some .cmd file and to remain in the same directory.
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
The problem is that while executing the setDomainEnv.cmd wit about 10 other cmd files called from it, I am left in totally another directory. And I want to be where I have started. As I always start in the same directory, I am adding a cd line:
Echo on
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Echo on
cd /d C:\workspaces\DS8\swprods\dist-4.1.8-local-devel\
No effect! Again I am left in
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server>
And I never even see the 3,4 lines to appear in the console. I could understand all that if some of the inner skripts ended with error, but they finish OK, without errors.
It would be understandable if some of the scripts changed the dir to another disk and back change would need /d key. But all is done on the same C: disk.
All operations in the script are conducted in the same source directory. The directory is changed after leaving the batch file.
....................................................
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if "Oracle" == "Apple" (set MEM
_ARGS=-Xms2048m -Xmx2048m -XX:CompileThreshold=8000 -XX:PermSize=128m -XX:MaxPermSize=256m -XX:MaxPermSize=256m )
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if exist C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd (call "C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd" )
^this is the last run line of the script
"Why?" and sometimes I thought, "Wherefore?" and sometimes I thought, "Inasmuch as which? And how can I change the directory back?
Use call:
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Call will invoke another script and after it gets completed - it will returns to your script.
If you do not use call - your execution flow goes to that another script but does not return back.
Use pushd to save the current directory then use popd to restore it after you run the other cmd file.
pushd .
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
popd
Good thing about pushd/popd is they work even if the other batch file changes the working directory to a directory on another drive.

gVim gives [Permission Denied] when using --remote-silent

I'm editing Unity 3D .cs files in gVim on Windows 7, I'd like when I double click a file in Unity for the file to open in an existing vim window, but instead it opens a new one.
To get around this I used various other SO answers to make a batch file and call vim using --remote-silent. This all works apart from vim acts like it wont allow remote commands to run on it.
Batch file:
#ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
start "" "C:\Program Files (x86)\Vim\vim73\gvim.exe" --remote-silent ":sp %1<CR>"
SHIFT
GOTO Loop
:Continue
It gives me
":sp C:\my\path\file.cs" [Permission Denied] Press enter or type command to continue
I tried removing the "%1" to check if it was the file that was giving the permission error, but no luck. The batch file runs via a shortcut that runs as administrator (nothing is ever simple in Windows)
Unity -> Shortcut -> Batch file -> gVim -> ":sp filename"
Any ideas why vim won't let this command run?
Can't seem to find any mention of permissions in vim help -remote files or anyone else seeing this on google.
Thanks.
*Edit 1
Looks like something weird si going on, if I do :ls I see this in my buffer list
2 %a= ":sp C:\My\file\path.cs" line 1
Like its treating the whole command as the file path or something?
--remote-silent takes a file list, not Ex commands (well, you can pass one via +{cmd} before the files); that's why Vim interprets your command as a (non-existing) file.
Using --remote-send is the correct approach; like in the help examples, you should prepend <C-\><C-N> to ensure that the remote Vim is in normal mode (otherwise, if you've left it in insert mode, the commands would be inserted literally!)
I solved this myself after spotting the buffer issue.
I changed my batch to the following:
#ECHO OFF
:Loop
IF "%1"=="" GOTO Continue
start "" "C:\Program Files (x86)\Vim\vim73\gvim.exe" --remote-send ":sp %1<CR>"
SHIFT
GOTO Loop
:Continue
Looks like --remote-silent doesn't do what I thought it did.
I also added set shortmess=aoOtI to my vimrc to stop it prompting me.

Using drag and drop from another directory onto a batch file does not work

I have a folder structure that looks like this:
project
bin
my_program.exe
misc_stuff.exe
DROP_OVER_ME.bat
input_file.txt
Basically, I want to be able to drag and drop the input file on top of the DROP_OVER_ME.bat batch file and have it pass on the path of the input file to the exe.
This is what my batch file looks like:
#echo off
start bin/my_program.exe %1
exit
When I drag and drop input_file.txt over the batch file, everything works fine -- my_program.exe successfully receives the path of the input file and runs.
However, when input_file.txt is located outside the project folder, dragging and dropping it makes the batch file throw up a popup message saying
Windows cannot find 'bin/my_program.exe'. Make sure you typed the name correctly, and then try again.
How can I fix my batch file so I can drag and drop files from any arbitrary location inside my filesystem?
Sounds like the batch script is basing the current working directory as the directory you're dragging from, and not the directory containing the script. (You can test this by adding an echo %cd% && pause to your script if you wish.) Try modifying your script as follows to remove any ambiguity about file paths:
#echo off
cd /d "%~dp0"
start "" "bin\my_program.exe" "%~f1"
exit /b

Pause command if not succeeded instead of automatically closing

Lets say I want to copy a file or run a program, but the file or the location is not found. How can I let my computer pause the command window when these kind of problems occure? I made a batch file to copy files to another location, but also to run programs.
Any help will be appreciated.
In your batch script, use xcopy to do the actual copying, rather than the copy command -
xcopy, unlike copy, returns error codes depending on the result of the copy, which are documented here in the remarks section.
As Aleksandr mentioned, you can use the error codes as part of your script to pause on error.
Assuming you would like to pause on all errors, you could do script this as below:
xcopy /HECY <source> <destination>
if %ERRORLEVEL% NEQ 0 pause
Obviously you'll need to replace and in the above with the locations you are copying from and to respectively.
The /HECY switches are just an example, but in this case could be used to instruct xcopy to copy hidden files, recurse directories, and automatically overwrite files in the destination if they exist. You can tweak these to your specific needs.
Check the error code and pause if it is non zero

Resources