1

Looking for a beginner friendly approach for using touch in windows

I saw this @stack and even tried googling a couple of times , but most of it seemed to create more confusion rather than solve my existing crisis.

Finally I ended up making my own function in powershell which worked similar to creating a file using touch in linux

Jo_L
  • 189
  • 1
  • 11
  • The post you link to isn't PowerShell-specific (and partially relies on `cmd.exe`-_internal_ commands), but there are several duplicates that are (see the list). – mklement0 Jul 09 '21 at 14:43
  • @mklenment0 i have less than 15 reputation , I guess that was the reason i was not able post ans from this account – Jo_L Jul 09 '21 at 14:49
  • but there are several duplicates that are (see the list). – mklement0 ; yes, but as of now (when i made the post) there is still not a right ans chosen by the one who posted the question – Jo_L Jul 09 '21 at 14:52
  • You're correct, @Jo_L, my bad: Even the self-answer feature requires 15 reputation points. – mklement0 Jul 09 '21 at 14:53
  • That a question doesn't have an accepted answer is no reason to create a duplicate question - instead, post an answer to the original, if you think you found a better solution (from what I understand, you don't need reputation for that). – mklement0 Jul 09 '21 at 14:59
  • post an answer to the original, @mklenment0 ; I tried that as well. But stack said that they dont accept answers from me anymore – Jo_L Jul 09 '21 at 15:06
  • Well, that is a temporary suspension, explained [here](https://meta.stackoverflow.com/questions/255583/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th). I suggest following the guidance there rather than circumventing the rules by creating a second account. – mklement0 Jul 09 '21 at 15:11
  • I tried everything as per they said too `The only way for the ban to be lifted is by contributing positively to the site` but seems like none of my ans are being noticed and therefore not getting a positive reputation and so not able to post ans – Jo_L Jul 09 '21 at 15:16
  • You quoted the relevant passage regarding the ban (I misspoke when I called it temporary). Again, I suggest not circumventing the rules, and I therefore encourage you to delete the [second account](https://stackoverflow.com/users/16003320/not-joel) you've used to post your answer. If you feel the ban was unjustified, I encourage you to plead your case on [meta]. – mklement0 Jul 09 '21 at 21:19
  • Useful suggessions like [this](https://stackoverflow.com/questions/44867313/how-do-i-read-input-in-files-in-command-line-on-windows/67654378#67654378) are done through the 2nd account of mine. Even that doesnt seem to catch anyones eye and not getting reputation even there. I kind of use that now as my main account in stack since my primary account doesnt seem to be of any use in stack – Jo_L Jul 10 '21 at 08:31
  • The linked answer is useful (I just up-voted it), and I encourage you to keep contributing - there's no telling _when_ answers are noticed and get up-votes. – mklement0 Jul 10 '21 at 13:04

1 Answers1

1

Read with touch in Powershell using functions

Try creating the below function in powershell

function touch{ ni $args } 

ni -> New-Item | if ni doesnt yeild an ans try New-Item instead

which works perfectly

Now you can try out touch as in the image below to create files

single arg

multiple arg

But does this mean that you would have to create a function every time you need to use?? Well, not at all my friendos

there is a better way around that. All you have to do is to add it to a powershell profile.

You open your powershell profile with the following command

notepad $profile

All you need to do now is to add the above function to this file and save it. Now close all the powershell that you have open and reopen them.This time the function gets loaded when powershell starts and it works globally which means any instance of powershell can use it now.

a better advanced implementation of touch can be found here thanks to @mklement0

mklement0
  • 382,024
  • 64
  • 607
  • 775
Not Joel
  • 85
  • 10
  • 1
    You're welcome, @Jo_L. Your function is helpful, but I suggest noting its limitations in the answer: (a) it isn't PowerShell-idiomatic (no pipeline support, individual arguments instead of an array) and (b) as an emulation of the Unix `touch` utility it is incomplete: no support for updating the timestamps of _existing_ files (your function will simply fail), no support for wildcards (useful only for timestamp-updating). For a PowerShell-idiomatic solution that provides most of the Unix `touch` functionality, `Touch-File`, see [this answer](https://stackoverflow.com/a/58756360/45375). – mklement0 Jul 09 '21 at 15:05
  • Ohh I didnt know that. I thought that this would be helpful to most programmers and developers who are just trying to create a file in windows most of the time – Jo_L Jul 09 '21 at 15:11
  • If you want to improve your function to at least support the _core_ functionality of Unix `touch`, you can take inspiration from [this duplicate](https://stackoverflow.com/a/32451953/45375) (which is limited to _one_ file). – mklement0 Jul 09 '21 at 15:13
  • Note that to provide the functionality that is currently in your answer, all you need to do is `New-Item foo, bar, baz` to create files `foo`, `bar`, and `baz`. – mklement0 Jul 09 '21 at 15:16
  • Understood, but if you want to deviate from PowerShell conventions (multiple arguments for a _single_ parameter being passed as an _array_, with `,` as the separator), the only good reason to do so is to emulate an _existing utility_ - and if you do so, you should at least support its _core_ functionality, not just _one_ behavior (file _creation_). As an aside: `function touch { ni $args }` is enough to implement what you're trying to do, but I don't think a wrapper function for `New-Item` whose only purpose is to allow you to pass arguments individually rather than as an array is worth it. – mklement0 Jul 09 '21 at 15:37
  • New-Item foo, bar, baz to create files foo, bar, and baz @mklement0 ; yes ni is alias for New-Item in powershell . I wanted to call touch seperated by spaces instead of commas `,` like : `touch foo bar baz` to create files `foo`, `bar` and `baz` – Jo_L Jul 09 '21 at 15:44
  • function touch { ni $args } is enough to implement what you're trying to do ; You are right but it doesnt work well with multiple arguments. I will look at an array approach as you suggested. All the array approach I tried either failed misrably or got too complicated. As the zen of python goes : `Simple is better than complicated`. I will try to find a simple array approach if I can – Jo_L Jul 09 '21 at 15:54
  • `touch { ni $args }` _does_ work with multiple arguments, but the main point is that such an _incomplete_ emulation of the `touch` utility isn't worth implementing in my estimation. I've just posted an emulation that _is_ worth implementing, because it at least covers the _core_ `touch` functionality, in [this answer](https://stackoverflow.com/a/68320161/45375). As mentioned before, a near-complete emulation that is also PowerShell-idiomatic is in [this answer](https://stackoverflow.com/a/58756360/45375). – mklement0 Jul 09 '21 at 16:42
  • I respect your ans but i still think mine is simpler . For people who are too busy and have no time to waste, I believe this ans would be helpful and easy to understand even if they dont understand powershell very well. And also I tried `ni $args` and it works now. thanks – Jo_L Jul 09 '21 at 19:16
  • (As an aside: you'll have to @-mention me to notify me of follow-up comments.) Your answer is simple, yes, but _it shouldn't be called `touch`_, because that means making a promise it doesn't fulfill. `ni foo, bar, baz` is also simple, and doesn't even require a function. Time will tell whether other people see value in your function (as reflected in up-votes) - I personally do not. And people who don't understand PowerShell well should be guided toward understanding it better, which is well worth the effort - without that, they'll end up wasting much more time in the long run. – mklement0 Jul 09 '21 at 21:00
  • The Question is in the duplicate part and so it has been closed by stack – Not Joel Jul 31 '21 at 08:33