batch-file

Sort file name with delimiter in batch file

一曲冷凌霜 提交于 2020-01-25 13:05:48
问题 I need to sort the list of file names by their version number with least first, in windows batch script. The file names are like: 2_0_0to2_0_1 2_0_1_to2_0_2 ... 2_0_12_to2_0_13 ... I've tried dir and sort in Windows but it seems to only look at char positions which wouldn't work in the case of double digits. In Linux, I've done this with: ls *.txt | sort -n -t _ -k1 -k2 -k3. How to do this on Windows. Please help. Thanks! 回答1: Try this: @ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION FOR %%x IN (*

Results of perforce command to command line variable

筅森魡賤 提交于 2020-01-25 11:10:03
问题 I'm working within a batch file and I have a Perforce command to call in order to gain some user information. The command is: p4 -Ztag -F %clientName% info Now what I need is for the result of this command to be held in either a variable or a file (ideally the former). But for the life of me I can't figure out how to do so, can someone please help me out. I gave the following command a try: p4 -Ztag -F %clientName% info > %HOMEPATH%\clientName.txt But the results were: p4 -Ztag -F info 1>

Run a Powershell script from Batch file with elevated privileges?

假如想象 提交于 2020-01-25 10:33:06
问题 I want to run example.ps1 from code.bat I realize that the start command would run it but require that it be run as admin, how can I do this? 回答1: In your batch file: powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}" ...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions. Full explanation: Start-Process can be used to run a program, and also has

How do I make an IF statement based on which keys are pressed in batch or powershell?

杀马特。学长 韩版系。学妹 提交于 2020-01-25 07:48:06
问题 I recently got an awesome mouse and found the back & forward buttons nearly useless. However, they were well placed so I decided to try and make a little program. Is it possible to make a PowerShell/Batch program that will: 1. Deploy an if statement that will be triggered by a keyboard shortcut (ALT + RIGHT & ALT + LEFT) 2. Once the if statement is triggered, reject the keys as if nothing happened (most likely by sending the keys to the script instead of the current program) 3. Send new keys

Sorting Numbers within an array [Batch Script]

感情迁移 提交于 2020-01-25 07:27:07
问题 I'm trying to create an array that sorts numbers. It doesn't give me an error message but instead an infinite loop of the input numbers trying to do something. I need to sort the numbers in the array using this method but I can't seem to find where I went wrong. @echo off setlocal enabledelayedexpansion set n= set n=1 rem *** Section 1 "Input data into num array" :Input set v= set /p v=enter a value: if !v!== goto Input set /a v=!v! set num[!n!]= set num[!n!]=!v! set /a n=!n!+1 if !n! lss 6

Windows batch script nested for loop pass current filename from innerloop to another command

风流意气都作罢 提交于 2020-01-25 06:41:28
问题 Really struggling with double quotes, double percent signs etc in batch script. So i have a folder, lets call it C:\EncryptedFiles It can have one or more subfolders, and each subfolder can have one or more encrypted file (with extension gpg). I need to look at everything inside C:\EncyptedFiles folder and iterate over files in each subfolder, and decrypt those files in same place where encrypted file is there. So if we have another folder called Subfolder1 with file EncryptedFile1.csv.gpg,

How to copy only files(not directories) using batch file?

一世执手 提交于 2020-01-25 06:32:30
问题 The directory structure is: Images -Folder1 -image1.jpg -image2.jpg -Folder2 -image3.jpg -image4.png -Folder3 -image6.png -image7.jpg -Folder4 I want to copy all images(i.e *.jpg, *.png) files only (not the folders) into the parent directory("Images"). I have tried using "robocopy" as follows: robocopy /np ..\..\Exam\Images ..\..\Exam\Images *.jpg *.png /S Here all files as well as folders are copied :(. But I need only files to be copied. How to do this? Many many thanks in advance! 回答1: Try

Windows Batch: Split String to individual characters to variables

丶灬走出姿态 提交于 2020-01-25 05:36:30
问题 in Windows Batch, if I had a variable (length can change) that was, for example: "hello world!" is it possible to "split" the variable so each character is its own variable so the output could look like: t1=h t2=e t3=l etc. Any help would be appreciated. 回答1: Use this code: setlocal EnableDelayedExpansion set str="hello world^!" set tempstr=%str% set count=0 :loop if defined tempstr ( set tempstr=%tempstr:~1% set /a count+=1 set /a pos=%count%-1 set t!count!=!str:~%pos%,1! goto loop ) ::

Add a single file to each rar file in a folder

帅比萌擦擦* 提交于 2020-01-25 03:53:05
问题 I'm trying to figure out how to add a file in each .rar file in a folder. For example I have: rar1.rar rar2.rar rar3.rar And I want the 'readme.txt' file added to each of them. Or if that isn't possible I can just extract all the .rar files turning them into folder then use this batch code to compress them? How do I include the readme.txt file? @ECHO OFF cd C:\Users\userss\Desktop\compressing SET PATH=C:;C:\Program Files\WinRAR;C:\Windows\system32;C:\Windows;C:\Win dows\System32\Wbem;%PATH%

Fix a batch script to handle closing parentheses inside an if block

霸气de小男生 提交于 2020-01-25 03:10:12
问题 I have a batch script to run different PHP versions under different environments. @ECHO OFF setlocal EnableExtensions EnableDelayedExpansion IF "%ANSICON%" == "" ( php7 %* ) ELSE ( php5 %* ) The problem is it breaks on the first unescaped closing parenthesis as it matches the opening parenthesis in IF "%ANSICON%" == "" ( . C:\>php -r echo'()'; ' was unexpected at this time. C:\>php -r echo'(())'; )' was unexpected at this time. The line setlocal EnableExtensions EnableDelayedExpansion is new