I attempted several tests, running a Perl command and a Command Prompt command on Git Bash, but they were not successful due to the errors.
Remember that I can not use PowerShell on my company's computer because it is blocked. I can use only Command Prompt and Git Bash, but I need only Git Bash and a Bash script. I do not want a Batch file.
I referred to:
How to get system user full name on Windows in Perl?
Print the full name of user in batch
I tested on a Shell Script file on Git Bash:
MSYS_NO_PATHCONV=1 net user %USERNAME% /domain |find /I "Nome completo"
I received the error:
find: ‘/I’: No such file or directory
find: ‘Nome completo’: No such file or directory
Could not find username.
For more info, type NET HELPMSG 2221.
Also:
perl -MWin32API::Net -E "Win32API::Net::UserGetInfo('',Win32::LoginName(), 10, my $info = {} ); say $info->{fullName}"
But I received the error:
Can't locate Win32API/Net.pm in #INC (you may need to install the Win32API::Net module) (#INC contains: /usr/lib/perl5/site_perl /usr/share/perl5/site_perl /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/perl5/core_perl).
BEGIN failed--compilation aborted.
Is there how to get a full name of a current Windows user on a Bash/Shell script on Git Bash?
In a bash shell, find would be /usr/bin/find, that is C:\Program Files\Git\usr\bin\find.exe, not C:\Windows\System32\find.exe.
In that context (Git bash), you can use grep instead:
MSYS_NO_PATHCONV=1 net user ${USERNAME} /domain | grep -i "Nome completo"
I followed "How do I get the current user's username in Bash?", but the environment variable USERNAME should still be visible in Git bash.
The syntax is just different.
In my Visual Studio Pre-built command line I have a newly created line
Powershell.exe -file "$(SolutionDir)Folder1\MyPowerShell.ps1"
And in this PowerShell script, it runs an .exe with some parameters
.\MyProgram.exe .\SomeFileArg.xml
When I run just the PowerShell script locally, either as ./MyPowerShell.ps1 or copy pasting the command into my PowerShell, it works.
But when I build my Visual Studio, this doesn't occur. I get the error .\MyProgram.exe.exe : The term '.\MyProgram.exe' is not recognized as the name of a cmdlet, 2> function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 2> path is correct and try again. If I include the absolute path instead in my PowerShell script, then it works.
How can I achieve this with local paths? Or is there a variable so I don't have to hardcode the absolute path?
You can declare the local path (where the script file resides) as a variable in your code using:
$PSScriptRoot
https://riptutorial.com/powershell/example/27231/-psscriptroot
From what I understand bin\bash.exe invokes usr\bin\bash.exe I guess with some options to help manage windows paths but can't find any documentation to indicate that.
All I know is my scripts seem not to work as expected if I have my environment set to use Git\usr\bin\ rather than Git\bin.
A new dev box seems to have this setup:
where bash
C:\Windows\System32\bash.exe
C:\Program Files\Git\usr\bin\bash.exe
I can understand wsl being top but can't understand the second entry if bin/bash.exe is the one I am supposed to use?
%windir%\system32\bash.exe = inline bash shell hosted by whatever windows subsystem for linux [ wsl ] environment you have enabled using microsoft store | windows subsystem for linux | | install. An alternative but essentially same result as the separate window that start menu shortcut %windir%\system32\wsl.exe ~ -d Ubuntu launches.
%programfiles%\git\bin\bash.exe -> %programfiles%\git\usr\bin\bash.exe + some automatically injected arguments = inline [ main stdin/stdout ] bash shell provided by git layered on top of windows cmd.exe command prompt environment
%programfiles%\git\git-bash.exe = windows app [ winmain ] bash shell provided by git layered on top of windows cmd.exe command prompt environment
So it appears that:
"C:\Program Files\Git\usr\bin\bash" -li
yields:
Me#MYPC MSYS /usr/bin
and
"C:\Program Files\Git\bin\bash" -li
yields:
Me#MYPC MINGW64 /usr/bin
So to my eyes that implies the EXE are compiled differently.
But in my case the issues I was having with my scripts was purely down to the PATH each exe sets up:
For
"C:\Program Files\Git\usr\bin\bash.exe"
$ where FIND
C:\Windows\System32\find.exe
C:\Program Files\Git\usr\bin\find.exe
Whereas:
"C:\Program Files\Git\bin\bash.exe"
$ where FIND
C:\Program Files\Git\usr\bin\find.exe
C:\Windows\System32\find.exe
So my script in the former case was failing with a
FIND: Parameter format not correct
as it wasn't finding the correct version of find.
Powershell suddenly quit opening from both cmd and powershell prompts. I haven't installed anything new between when it did work and when it quit working.
When I try to start powershell.exe from a cmd window (both elevated and not elevated) with the following command
C:\Users\myuser>powershell.exe
I get a popup error from the OS that says:
This app can't run on your PC
Once I close that popup the cmd prompt I made the call from then prints:
Access is denied
To the screen (yes even when I do this in an elevated cmd prompt)
When I try to do it in powershell with the following command:
PS C:\Users\myuser> powershell.exe
I get:
Program 'powershell.exe' failed to run: The specified executable is not a valid application for this OS platform.
At line:1 char:1
+ powershell.exe
+ ~~~~~~~~~~~~~~.
At line:1 char:1
+ powershell.exe
+ ~~~~~~~~~~~~~~.
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorID : NativeCommandFailed
Apparently even powershell doesn't like powershell anymore.
I've tried restarting the computer and that didn't fix it, but I'm totally stymied as to what to do next.
#PetSerAl gave the crucial pointer in comments on the question.
The "This app can't run on your PC" pop-up error message on Windows 8 or above
indicates:
a corrupted file, such as a 0-byte *.exe file, esp. when followed by an "Access denied" error in the console.
or, increasingly less commonly, an attempt to run a 64-bit executable on a 32-bit edition of Windows.
Troubleshooting steps:
From a Command Prompt (cmd.exe console), run where.exe <executable-name>;
from PowerShell, run Get-Command -All <executable-name>, which shows you all executables by that name present in the directories listed in the $env:PATH environment variable in that order, by their full paths.
Note that where.exe, unlike Get-Command, also looks in the current directory, and looks there first.
Thus, the first path returned is the executable that is actually executed when only the executable name is specified.
Note that a match in the current directory, if found by where.exe, only matters when calling the executable from cmd.exe (from the Command Prompt or a batch file), because PowerShell by design doesn't allow invocation of executables from the current directory by mere name.
If you want to run where.exe from PowerShell, extension .exe is required, because the command name where by itself is a built-in alias for the Where-Object cmdlet.
In the output from where.exe / Get-Command, check:
if the executable you expect is listed first.
if its size is non-zero.
Remove unexpected (zero-byte) executables, or, if you expect them to be there as functioning executables, reinstall them.
Example:
Look for all all executables named powershell.exe in the current directory and in the directories listed in $env:PATH.
Note that the proper home of powershell.exe is C:\Windows\System32\WindowsPowerShell\v1.0, as reflected in $PSHOME.
From cmd.exe (regular Command Prompt):
where powershell.exe
Example output:
C:\Windows\System32\powershell.exe
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
From PowerShell:
Get-Command -All powershell.exe
If you also want to look in the current directory, use
Get-Command -All .\powershell.exe, powershell.exe
Example output:
CommandType Name Version Source
----------- ---- ------- ------
Application powershell.exe 0.0.0.0 C:\WINDOWS\system32\powershell.exe
Application powershell.exe 10.0.14... C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe
If you want to include the file size in the output:
PS> where.exe powershell.exe | % { [system.io.fileinfo] $_ |
select fullname, length, #{ n = 'Version'; e = { $_.versioninfo.FileversionRaw } } }
FullName Length Version
-------- ------ -------
C:\Windows\System32\powershell.exe 0
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe 446976 10.0.14393.206
Delete the powershell.exe (with 0KB) from location C:\Windows\System32
In my case Powershell working fine after deleting the powershell.exe (with 0KB) from system 32
On a windows machine I get this error
'touch' is not recognized as an internal or external command, operable program or batch file.
I was following these instructions which seem to be linux specific, but on a standard windows commandline it does not work like this:
touch index.html app.js style.css
Is there a windows equivalent of the 'touch' command from the linux / mac os / unix world ? Do I need to create these files by hand (and modify them to change the timestamp) in order to implement this sort of command? I am working with node and that doesn't seem very ... node-ish...
An easy way to replace the touch command on a windows command line like cmd would be:
type nul > your_file.txt
This will create 0 bytes in the your_file.txt file.
This would also be a good solution to use in windows batch files.
Another way of doing it is by using the echo command:
echo.> your_file.txt
echo. - will create a file with one empty line in it.
If you need to preserve the content of the file use >> instead of >
> Creates a new file
>> Preserves content of the file
Example
type nul >> your_file.txt
You can also use call command.
Calls one batch program from another without stopping the parent batch program. The call command accepts labels as the target of the call.
Example:
call >> your_file.txt
---
or even if you don't want make it hard you can Just install Windows Subsystem for Linux (WSL). Then, type.
wsl touch
or
wsl touch textfilenametoedit.txt
Quotes are not needed.
Windows does not natively include a touch command.
You can use any of the available public versions or you can use your own version. Save this code as touch.cmd and place it somewhere in your path
#echo off
setlocal enableextensions disabledelayedexpansion
(for %%a in (%*) do if exist "%%~a" (
pushd "%%~dpa" && ( copy /b "%%~nxa"+,, & popd )
) else (
type nul > "%%~fa"
)) >nul 2>&1
It will iterate over it argument list, and for each element if it exists, update the file timestamp, else, create it.
You can use this command: ECHO >> filename.txt
it will create a file with the given extension in the current folder.
UPDATE:
for an empty file use: copy NUL filename.txt
On windows Power Shell, you can use the following command:
New-Item <filename.extension>
or
New-Item <filename.extension> -type file
Note: New-Item can be replaced with its alias ni
The answer is wrong, it only works when the file does not exist. If the file exists, using the first does nothing, the second adds a line at the end of the file.
The correct answer is:
copy /b filename.ext +,,
I found it here: https://superuser.com/questions/10426/windows-equivalent-of-the-linux-command-touch/764721#764721
I'm surprised how many answers here are just wrong. Echoing nothing into a file will fill the file with something like ECHO is ON, and trying to echo $nul into a file will literally place $nul into the file. Additionally for PowerShell, echoing $null into a file won't actually make a 0kb file, but something encoded as UCS-2 LE BOM, which can get messy if you need to make sure your files don't have a byte-order mark.
After testing all the answers here and referencing some similar ones, I can guarantee these will work per console shell. Just change FileName.FileExtension to the full or relative-path of the file you want to touch; thanks to Keith Russell for the COPY NUL FILE.EXT update:
CMD w/Timestamp Updates
copy NUL FileName.FileExtension
This will create a new file named whatever you placed instead of FileName.FileExtension with a size of 0 bytes. If the file already exists it will basically copy itself in-place to update the timestamp. I'd say this is more of a workaround than 1:1 functionality with touch but I don't know of any built-in tools for CMD that can accomplish updating a file's timestamp without changing any of its other content.
CMD w/out Timestamp Updates
if not exist FileName.FileExtension copy NUL FileName.FileExtension
Powershell w/Timestamp Updates
if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file} else {(ls FileName.FileExtension ).LastWriteTime = Get-Date}
Yes, it will work in-console as a one-liner; no requirement to place it in a PowerShell script file.
PowerShell w/out Timestamp Updates
if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file}
Use the following command on the your command line:
fsutil file createnew filename requiredSize
The parameters info as followed:
fsutil - File system utility ( the executable you are running )
file - triggers a file action
createnew - the action to perform (create a new file)
filename - would be literally the name of the file
requiredSize - would allocate a file size in bytes in the created file
install npm on you machine
run the below command in you command prompt.
npm install touch-cli -g
now you will be able to use touch cmd.
You can replicate the functionality of touch with the following command:
$>>filename
What this does is attempts to execute a program called $, but if $ does not exist (or is not an executable that produces output) then no output is produced by it. It is essentially a hack on the functionality, however you will get the following error message:
'$' is not recognized as an internal or external command,
operable program or batch file.
If you don't want the error message then you can do one of two things:
type nul >> filename
Or:
$>>filename 2>nul
The type command tries to display the contents of nul, which does nothing but returns an EOF (end of file) when read.
2>nul sends error-output (output 2) to nul (which ignores all input when written to). Obviously the second command (with 2>nul) is made redundant by the type command since it is quicker to type. But at least you now have the option and the knowledge.
as mentioned
echo >> index.html
it can be any file, with any extension
then do
notepad index.html
this will open your file in the notepad editor
No command – neither typenor echo– is necessary to emulate Unix's/Mac OS X's 'touch' command in a Windows Powershell terminal. Simply use the following shorthand:
$null > filename
This will create an empty file named 'filename' at your current location. Use any filename extension that you might need, e.g. '.txt'.
Source: https://superuser.com/questions/502374/equivalent-of-linux-touch-to-create-an-empty-file-with-powershell (see comments)
For a very simple version of touch which would be mostly used to create a 0 byte file in the current directory, an alternative would be creating a touch.bat file and either adding it to the %Path% or copying it to the C:\Windows\System32 directory, like so:
touch.bat
#echo off
powershell New-Item %* -ItemType file
Creating a single file
C:\Users\YourName\Desktop>touch a.txt
Directory: C:\Users\YourName\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020-10-14 10:28 PM 0 a.txt
Creating multiple files
C:\Users\YourName\Desktop>touch "b.txt,c.txt"
Directory: C:\Users\YourName\Desktop
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 2020-10-14 10:52 PM 0 b.txt
-a---- 2020-10-14 10:52 PM 0 c.txt
Also
Works both with PowerShell and the Command Prompt.
Works with existing subdirectories.
Does not create a file if it already exists:
New-Item : The file 'C:\Users\YourName\Desktop\a.txt' already exists.
For multiple files, creates only the files that do not exist.
Accepts a comma-separated list of filenames without spaces or enclosed in quotes if spaces are necessary:
C:\Users\YourName\Desktop>touch d.txt,e.txt,f.txt
C:\Users\YourName\Desktop>touch "g.txt, 'name with spaces.txt'"
You can also use copy con [filename] in a Windows command window (cmd.exe):
C:\copy con yourfile.txt [enter]
C:\CTRL + Z [enter] //hold CTRL key & press "Z" then press Enter key.
^Z
1 Files Copied.
This will create a file named yourfile.txt in the local directory.
I use cmder (a command line emulator)
It allows you to run all Linux commands inside a Windows machine.
It can be downloaded from https://cmder.net/
I really like it
From the Terminal of Visual Code Studio on Windows 10, this is what worked for me to create a new file:
type > hello.js
echo > orange.js
ni > peach.js
As Raghuveer points out in his/her answer, ni is the PowerShell alias for New-Item, so you can create files from a PowerShell prompt using ni instead of touch.
If you prefer to type touch instead of ni, you can set a touch alias to the PowerShell New-Item cmdlet.
Creating a touch command in Windows PowerShell:
From a PowerShell prompt, define the new alias.
Set-Alias -Name touch -Value New-Item
Now the touch command works almost the same as you are expecting. The only difference is that you'll need to separate your list of files with commas.
touch index.html, app.js, style.css
Note that this only sets the alias for PowerShell. If PowerShell isn't your thing, you can set up WSL or use bash for Windows.
Unfortunately the alias will be forgotten as soon as you end your PowerShell session. To make the alias permanent, you have to add it to your PowerShell user profile.
From a PowerShell prompt:
notepad $profile
Add your alias definition to your profile and save.
If you have Cygwin installed in your PC, you can simply use the supplied executable for touch (also via windows command prompt):
C:\cygwin64\bin\touch.exe <file_path>
Assuming the file exists and you just need to update the timestamp.
type test.c > test.c.bkp && type test.c.bkp > test.c && del test.c.bkp
Use rem. > file.txt (notice the dot attached to the command "rem")
this creates an empty file
Shortest possible vanilla solution is :
.>myfile.txt
You will get an error , but file is created :
If you are using VS Code, there is a command line tool code to help you open a non-exist file in VS Code.
There is something missing in all of the other answers. The Linux touch command has a -t option, which lets you set the last modified time to any arbitrary date and time, not just the current time.
This sets the modification date of filename.txt to 20 December 2012 at 30 minutes after midnight.
touch -t 201212210030 filename.txt
To get the equivalent in Windows, you need to be in PowerShell, it can't be done in Cmd.
Change the creation date/timestamp of a file named filename.txt:
(Get-Item "D:\Test\filename.txt").CreationTime=("21 December 2012 00:30:00")
Change the last write date/timestamp of a file named filename.txt:
(Get-Item "D:\Test\filename.txt").LastWriteTime=("21 December 2012 00:30:00")
Change the last accessed date/timestamp of a file named filename.txt:
(Get-Item "D:\Test\filename.txt").LastAccessTime=("21 December 2012 00:30:00")
Using PowerShell, type: ni index.html or ni style.css or ni app.js
ni <filename>.<extension>
If you have WSL with the appropriate distro like Ubuntu you can have the touch command in the CMD and not just the bash terminal. It works for me on Windows 10 & 11 Windows Terminal
Easy, example with txt file
echo $null >> filename.txt
Yes you can use Node for Touch I just use that and its working all fine in windows Cmd or gitbash
Use type instead of touch
type YOUR_FILE_NAME
However, it is limited to just a single file