IT박스

'touch'와 같은 Windows (즉 index.html을 만드는 node.js 방법)

itboxs 2020. 6. 13. 19:31
반응형

'touch'와 같은 Windows (즉 index.html을 만드는 node.js 방법)


Windows 컴퓨터 에서이 오류가 발생합니다

'touch'는 내부 또는 외부 명령, 작동 가능한 프로그램 또는 배치 파일로 인식되지 않습니다.

내가 지시따라야 할 때 :

touch index.html app.js style.css

'touch'를 사용하는 것과 동등한 창이 있습니까? 이러한 종류의 명령을 구현하려면 이러한 파일을 수동으로 작성하고 시간 소인을 변경하도록 수정해야합니까? 그것은 ... 노드처럼 보이지 않습니다 ...


cmd 창에서 다음을 입력하십시오.

type nul > your_file.txt

your_file.txt 파일에 0 바이트가 생성됩니다.

다른 방법은 echo 명령을 사용하는 것입니다 .

echo.> your_file.txt

에코. -빈 줄이 하나있는 파일을 만듭니다.

2019-04-01에 편집 :

파일 내용을 보존해야하는 경우> 대신>를 사용하십시오.

>   Creates a new file
>>  Preserves content of the file

type nul >> your_file.txt

Windows에는 기본적으로 touch명령이 포함되어 있지 않습니다 .

사용 가능한 공개 버전 중 하나를 사용하거나 고유 한 버전을 사용할 수 있습니다. 이 코드를 다른 이름으로 저장 touch.cmd하고 경로 어딘가에 배치하십시오

@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

인수 목록을 반복하고 존재하는 경우 각 요소에 대해 파일 시간 소인을 업데이트하거나 그렇지 않으면 작성하십시오.


이 명령을 사용할 수 있습니다 : ECHO >> filename.txt

현재 폴더에 주어진 확장자를 가진 파일을 만듭니다.

최신 정보:

빈 파일 사용 : copy NUL filename.txt


대답이 잘못되었습니다. 파일이 존재하지 않을 때만 작동합니다. 파일이 존재하는 경우, 첫 번째 방법은 아무 것도 사용하지 않고 두 번째 파일은 파일 끝에 줄을 추가합니다.

정답은 다음과 같습니다.

copy /b filename.ext +,,

나는 그것을 여기에서 발견했다 : https://superuser.com/questions/10426/windows-equivalent-of-the-linux-command-touch/764721#764721


명령 행에서 다음 명령을 사용하십시오.

fsutil file createnew filename  requiredSize

다음과 같은 매개 변수 정보 :

fsutil-파일 시스템 유틸리티 (실행중인 실행 파일)

file-파일 작업을 트리거합니다

createnew-수행 할 조치 (새 파일 작성)

filename-말 그대로 파일 이름

requiredSize-작성된 파일에서 파일 크기를 바이트 단위로 할당합니다


나는 여기에 얼마나 많은 대답이 틀렸는 지 놀랐다 . 파일에 아무것도 넣지 않으면 파일이와 같이 채워지고 파일 ECHO is ON에 반향하려고 $nul하면 문자 그대로 파일에 배치 $nul됩니다. 또한 PowerShell의 $null경우 파일로 에코 하면 실제로 0kb 파일이 아니라로 인코딩 된 UCS-2 LE BOM파일이 생성되므로 파일에 바이트 순서 표시 가 없어야 하는 경우 지저분해질 수 있습니다.

여기에있는 모든 답변을 테스트하고 유사한 답변을 참조한 후에는 콘솔 셸별로 작동 할 수 있습니다. 그냥 변경 FileName.FileExtension당신이 원하는 파일의 전체 또는 상대 경로 touch:

CMD

if not exist FileName.FileExtension (fsutil file CreateNew FileName.FileExtension 0) else (copy /b FileName.FileExtension +,,)

이렇게하면 FileName.FileExtension길이가 0 바이트가 아닌 배치 한 파일이 새 파일로 생성 됩니다. 타임 스탬프를 업데이트하는 것 외에는 파일 else의 복사 작업에 이미 존재하는 경우 타임 스탬프를 업데이트하는 것 외에는 해당 파일에 아무런 작업도 수행하지 않습니다 touch. 나는 이것이 1 : 1 기능보다 더 많은 해결책이라고 말하고 싶지만 다른 내용을 변경하지 않고 파일의 타임 스탬프를 업데이트 할 수있는 CMD를위한 내장 도구를 모른다.

파워 쉘

if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file} else {(ls FileName .FileExtension ).LastWriteTime = Get-Date}

This has the same functionality as the CMD version. It won't mess with an existing file besides its timestamp, and it will create a brand new empty file of whatever you decided to use in place of FileName.FileExtension. And yes, it will work in-console as a one-liner; no requirement to place it in a PowerShell script file.

What if I don't want to change the timestamp?

If you don't want to modify the timestamp of an existing file, we can just remove the else-clauses.

CMD

if not exist FileName.FileExtension fsutil file CreateNew FileName.FileExtension 0

PowerShell

if (!(Test-Path FileName.FileExtension -PathType Leaf)) {New-Item FileName.FileExtension -Type file}


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.


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)


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


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.


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


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>

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

enter image description here

참고URL : https://stackoverflow.com/questions/30011267/windows-equivalent-of-touch-i-e-the-node-js-way-to-create-an-index-html

반응형