How to hide login script (batch file) while it is running? - batch-file

I have created login script using batch file. But actually I need this batch runs invisible (but still showing on the task manager).
As far that I can go is that I only able to minimize the batch file using below code:
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
#echo off
echo *Logon Script*
rem :Time_Set
rem echo Setting the system time...
rem net time \\EX001LT /set /y
rem echo.
I have tried using cmd /c "batchfile" and didn't work.

You can use vbs to hide the batch completely. Add the path to the logon script to MyScript.Run line.
Hidden.vbs
Set MyScript = CreateObject("WScript.Shell")
MyScript.Run "C:\path to batch\logonscript.cmd", 0, False
Then simply call the Hidden.vbs file instead of the batch file

Related

How can I find my error in the batch script?

The code below should archive some files by moving them into a subfolder. The batch file asks the user for the folder path. Then a subfolder should be created and if that was successful, it should move all files in the user input directory into the subdirectory. It works, but it closes although using pause. It does not output anything about a syntax error or anything at all. Please let me know if somebody notices something.
#echo off
SETLOCAL EnableDelayedExpansion
echo Insert path:
set /p path=
echo the path is %path%
cd %path%
echo The files will be moved to a new folder
pause
mkdir %path%\archived_files
IF EXIST "archived_files" (
for /f %%A in ('DIR /A /D /B') do (
echo %%A && move /Y %path%\%%A %path%\archived_files)
echo Folder "archived_files" created or already exists
) else ( echo Folder "archived_files" does not exist )
echo the files have been transferred
pause
ENDLOCAL
I suggest to use this batch file for the file moving task.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "BatchFileName=%~nx0"
set "BatchFilePath=%~dp0"
set "UserPath=%~1"
if defined UserPath goto ChangeFolder
:UserPrompt
set "UserPath="
set /P "UserPath=Enter path: "
rem Has the user not entered a string?
if not defined UserPath goto UserPrompt
rem Remove all double quotes from input string.
set "UserPath=%UserPath:"=%"
rem Has the user entered just one or more double quotes?
if not defined UserPath goto UserPrompt
:ChangeFolder
pushd "%UserPath%" 2>nul || (echo Folder "%UserPath%" does not exist.& goto UserPrompt)
for /F "eol=| delims=" %%I in ('dir /A-D /B 2^>nul') do goto CreateSubfolder
echo The folder does not contain any file to archive.& goto EndBatch
:CreateSubfolder
md "archived_files" 2>nul
if not exist "archived_files\" echo Failed to create subfolder: "archived_files"& goto EndBatch
rem It must be avoided that the currently running batch file is moved too.
set "ExcludeFileOption="
for %%I in ("%UserPath%\") do set "CurrentFolderPath=%%~dpI"
if "%CurrentFolderPath%" == "%BatchFilePath%" set "ExcludeFileOption= /XF "%BatchFileName%""
rem The command MOVE used with wildcard * does not move hidden files. A FOR loop
rem with MOVE is slow in comparison to usage of ROBOCOPY to move really all files.
rem The ROBOCOPY option /IS can be removed to avoid moving same files existing
rem already in the subfolder archived_files from a previous batch execution.
echo The files are moved to a new folder.
%SystemRoot%\System32\robocopy.exe . archived_files%ExcludeFileOption% /MOV /R:2 /W:5 /IS /NDL /NFL /NJH /NJS
if not errorlevel 2 if errorlevel 1 echo All files are moved successfully.
:EndBatch
popd
endlocal
pause
The batch file can be started with a a folder path as argument. So it is possible to right click on the batch file and click in opened context menu in submenu Send to on item Desktop (create shortcut). The .lnk file created on the userĀ“s desktop can be renamed now also via context menu or key F2 to whatever name is useful like Archive files. Then the shortcut file can be cut with Ctrl+X and pasted with Ctrl+V in the folder %APPDATA%\Microsoft\Windows\SendTo to have in Send to context submenu the menu item Archive files. This makes it possible to right click on a folder and click in opened context menu in submenu Send to on Archive files to run the batch file without the need to enter a folder path manually.
The batch file prompts the user for the path if not started with a folder path as first argument or the folder cannot be found at all. This user prompt is done using a safe method. The batch file makes the passed or entered folder temporarily the current folder for the remaining commands using PUSHD and POPD instead of CD to work also with UNC paths.
There is checked next if the folder contains any file at all. Otherwise the user is informed that the directory does not contain files to archive and batch file ends without any further action.
The file movement is done with ROBOCOPY for the reasons described in a remark in the batch file which requires Windows Vista or a newer Windows version or Windows Server 2003 or a newer Windows server version.
I recommend to see also:
Debugging a batch file which answers your question.
What is the reason for "X is not recognized as an internal or external command, operable program or batch file"? It explains why path as name for the environment variable to assign the user entered path is a really bad idea.
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? It explains the reasons for using the additional code to evaluate the string entered by the user.
Why is no string output with 'echo %var%' after using 'set var = text' on command line? It explains the recommended syntax for the (re)definition of an environment variable and why using this syntax.
Syntax error in one of two almost-identical batch scripts: ")" cannot be processed syntactically here describes several common issues made by beginners in batch file coding like not enclosing a file/folder path in double quotes.
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.
call /? ... explains %~nx0, %~dp0 and %~1 whereby argument 0 is always the batch file itself.
dir /?
echo /?
endlocal /?
for /?
goto /?
if /?
md /?
pause /?
popd /?
pushd /?
rem /?
robocopy /?
set /?
setlocal /?
Other useful documentations used to write this code:
single line with multiple commands using Windows batch file
the Microsoft documentations for the used Windows Commands
the SS64 documentations for the used Windows CMD commands, especially:
ROBOCOPY.exe
ROBOCOPY Exit Codes
the Microsoft documentation about Using command redirection operators
and the SS64 documentation How-to: Redirection
Note: The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line within ' appended as additional arguments.

Batch Script to copy folder from Server to user desktop in a network

My goal is to create a batch script to copy a folder with subfolders to user desktop with overwrite option and minimized of command prompt.
I am pushing the script through Group policy in user start up screen.
However I am getting an error when running the script locally. Not sure what I am missing in the script..
#echo off
#cls
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
echo Your Source Path:
set INPUT1=\\X.X.X.X\Test\TMS\
echo Your Destination Path:
set INPUT2=C:\Users\%Username%\Desktop\
xcopy %INPUT1% %INPUT2% /y /s
:minimized
You mentioned folder, so I am writing this assuming you want to create the TMS folder with content on the desktop.
I would try something like this inside of you minimized label. This is untested as I have no Network drives to test with.
for /f "tokens=2" %i in ('net use * "\\X.X.X.X\Test\TMS\" ^| findstr /i Drive') do set "tmpDr=%%i"
mkdir "%USERPROFILE%\Desktop\TMS" >nul
xcopy "%tmpDir%\*" "%USERPROFILE%\Desktop\TMS" /s /y
net use /d %tmpDir% >nul
Some things to note in your code. You have 2 minimized labels, you need to enclose path variables with double quotes to eliminate possible whitespace, you can drop the echo's seeing as you plan on running the script minimized. Last but not least, you do not need to specify full path to the user's desktop as %USERPROFILE% variable already exists as Drive:\Users\Username

Batch file doesn't complete on certain computers - Uses Google Developer Console

Problem
I have the following batch file (see below), on my own computer it runs successfully. It runs a Python executable compiled with GooeyParser, passes some variables to the batch script through temporary text files, then uses Google Dev Console to upload the file into GSC then BigQuery.
On my co-workers computer, the batch script just stops running after the gsutil command. There are no error messages, the script just stops running.
The individual commands run successfully on his computer.
Does anyone know why the batch script might only partially run?
Batch File
::spawns as a subprocess to avoid console window closing and losing error messages
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
::python script writes out file location to temporary file
"BQ Single File Upload - dont run by itself.exe"
SET /p FILEUPLOAD=<temp_file_path
SET /p FILENAME=<temp_filename
SET /p DATASET=<temp_dataset
SET /p TABLE=<temp_table
SET /p SCHEMA=<temp_schema
DEL temp_file_path
DEL temp_filename
DEL temp_dataset
DEL temp_table
DEL temp_schema
gsutil cp %FILEUPLOAD% "gs://our_data/%FILENAME%"
cmd.exe /c bq mk %DATASET%
bq load --max_bad_records=5 --skip_leading_rows=1 --allow_quoted_newlines --schema %SCHEMA% %DATASET%%TABLE% "gs://our_data/%FILENAME%"
PAUSE
Adding call, (thank you #Mofi). Here is the final script, which now works on both our computers.
::spawns as a subprocess to avoid console window closing and losing error messages
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )
::python script writes out file location to temporary file
"BQ Single File Upload - dont run by itself.exe"
SET /p FILEUPLOAD=<temp_file_path
SET /p FILENAME=<temp_filename
SET /p DATASET=<temp_dataset
SET /p TABLE=<temp_table
SET /p SCHEMA=<temp_schema
DEL temp_file_path
DEL temp_filename
DEL temp_dataset
DEL temp_table
DEL temp_schema
call gsutil cp %FILEUPLOAD% "gs://our_data/%FILENAME%"
call cmd.exe /c bq mk %DATASET%
call bq load --max_bad_records=5 --skip_leading_rows=1 --allow_quoted_newlines --schema %SCHEMA% %DATASET%%TABLE% "gs://our_data/%FILENAME%"
PAUSE

How to move a file without waiting to complete in Batch (this should be working)

Details:
Using a program called MCEBuddy to process video files and when completed MCEBuddy will run a users batch file for custom processing. In this batch file, I fix up a few things and simply move the proccessed video to it's final location.
Goal:
To allow the batch file to return to MCEBuddy and not wait until the move has completed. Currently MCEBuddy will not resume before the entire file (video) has been moved, I don't want that.
I have 2 batch files, the first simply passes the parameters to the second batch file.
I have search and search, and even asked the developer over at MCEBuddy and is seems that everything I have tried just doesn't work.
The first Batch file:
#ECHO OFF
setlocal EnableDelayedExpansion
SET BatFile="E:\VideoCaptures\Cleaned Files\TVTransfer.bat"
SET OutputFileName="%~n1"
SET InputFileName="%~n2"
SET FolderOutput="%~3"
SET WideoWidth="%~4"
SET OutputExtension="%~5"
SET OutputFileSize="%~z1"
SET OADMonth="%~7"
SET OADDay="%~8"
SET OADYear="%~9"
START "MCE Move" /b cmd /c Call %BatFile% %OutputFileName% %InputFileName% %FolderOutput% %WideoWidth% %OutputExtension% %OutputFileSize% %OADMonth% %OADDay% %OADYear%
The second Batch file (the part that moves the file)
START "MCE Move" /b cmd /c move /y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
I have also tried:
START "MCE Move" /MIN cmd /c move /Y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
and:
START "MCE Move" /MIN /b cmd /c move /Y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%
and many other iterations, but none will allow MCEBuddy to resume before the file has completed it's move.
What am I missing?
I'm on my phone right now. But have you tried
START "" "MCE Move" /b cmd /c move /y "%OUTPUT_FOLDER%\%~1%OUTPUT_Ext%" %sPAT%\%MMDDYYYY%

Batch file leaves vbscript program open in Wordpad. Why?

My batch file contains a START command that runs a short vbscript program. When the batch file completes, the code of the vbscript program is shown in an open Wordpad window. This only started happening after we converted to Windows 7. Never happened under XP. Why does this happen and how can I prevent it? I have done extensive internet searching and come up with nothing.
Here is batch file:
#echo off
cls
echo.
echo Copying Latest Version of FREDS Database ...
echo.
xcopy "\\sstore02\S-Drive.OOD\OPI\FREDS\FREDS.mdb" "K:\FREDS\" /i /q /y
echo.
echo If you see "1 File(s) copied" then the copy was successful
echo.
echo Copying Shortcut Installer ...
echo.
xcopy "\\sstore02\S-Drive.OOD\OPI\FREDS\FREDS-Shortcut.vbs" "K:\FREDS\" /i /q /y
echo.
echo If you see "1 File(s) copied" then the copy was successful
echo.
echo Adding Shortcut icon to Desktop ...
echo.
Start K:\FREDS\FREDS-Shortcut.vbs
echo.
pause
You need to call cscript instat of start.
cscript /nologo K:\FREDS\FREDS-Shortcut.vbs
The option /noscript hides the version of cscript in the output.
Notepad is the registered program for the VBS extension on your machine.
Right click a VBS file and select Open With and then navigate to cscript.exe in c:\windows\system32 folder usually. Select the checkbox to always use that program and then the start command will work with VBS scripts and Cscript.

Resources